Loading

libcurl 静态库编译

1.下载最新版的libcurl,并解压。(本例以vs2013 + curl-7.52.1.zip为例)

2.打开VS2013 开发人员命令提示,并CD进入 curl-curl-7_52_1/winbuild/ 目录

VS生成代码的时候有一个运行库选项(项目属性 - 配置属性 - C/C++ - 代码生成 - 运行库)/MT和/MD(/MTd和/MDd是对应的debug版本)

就是说你编译时如果加了RTLIBCFG=static这个选项,就相当于编译了/MT版本的libcurl,否则是/MD版本的

nmake /f Makefile.vc mode=static VC=12 RTLIBCFG=static

完成编译后libcurl静态库会出现在builds目录下包括头文件和libcurl_a.lib,我们需要的是builds/libcurl-vc12-x86-release-static-ipv6-sspi-winssl下面的include和lib两个文件夹

3.测试

项目->属性->c/c++ ->代码生成->运行库->/MT

// curl_test.cpp : 定义控制台应用程序的入口点。
#define CURL_STATICLIB
#include <iostream>
#include <curl.h>
#pragma comment(lib, "libcurl_a.lib")

using namespace std;

/**
* 一旦curl接收到数据,就会调用此回调函数
* buffer:数据缓冲区指针
* size:调试阶段总是发现为1
* nmemb:(memory block)代表此次接受的内存块的长度
* userp:用户自定义的一个参数
*/
size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
{
	static int current_index = 0;

	cout << "current:" << current_index++;
	cout << (char*)buffer;
	cout << "---------------" << endl;

	int temp = *(int*)userp;    // 这里获取用户自定义参数
	return nmemb;
}

int main()
{
	curl_global_init(CURL_GLOBAL_ALL); // 首先全局初始化CURL
	CURL* curl = curl_easy_init(); // 初始化CURL句柄

	if (NULL == curl)
	{
		return 0;
	}

	int my_param = 1;    // 自定义一个用户参数

	// 设置目标URL
	curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com");
	// 设置接收到HTTP服务器的数据时调用的回调函数
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
	// 设置自定义参数(回调函数的第四个参数)
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &my_param);
	// 执行一次URL请求
	CURLcode res = curl_easy_perform(curl);
	// 清理干净
	curl_easy_cleanup(curl);

	return 0;
}

配置选项:

nmake /f Makefile.vc mode=<static or dll> <options>

where <options> is one or many of:
  VC=<6,7,8,9,10,11,12,14>     - VC versions
  WITH_DEVEL=<path>            - Paths for the development files (SSL, zlib, etc.)
                                 Defaults to sibbling directory deps: ../deps
                                 Libraries can be fetched at http://windows.php.net/downloads/php-sdk/deps/
                                 Uncompress them into the deps folder.
  WITH_SSL=<dll or static>     - Enable OpenSSL support, DLL or static
  WITH_MBEDTLS=<dll or static> - Enable mbedTLS support, DLL or static
  WITH_CARES=<dll or static>   - Enable c-ares support, DLL or static
  WITH_ZLIB=<dll or static>    - Enable zlib support, DLL or static
  WITH_SSH2=<dll or static>    - Enable libSSH2 support, DLL or static
  ENABLE_SSPI=<yes or no>      - Enable SSPI support, defaults to yes
  ENABLE_IPV6=<yes or no>      - Enable IPv6, defaults to yes
  ENABLE_IDN=<yes or no>       - Enable use of Windows IDN APIs, defaults to yes
                                 Requires Windows Vista or later, or installation from:
                                 https://www.microsoft.com/downloads/details.aspx?FamilyID=AD6158D7-DDBA-416A-9109-07607425A815
  ENABLE_WINSSL=<yes or no>    - Enable native Windows SSL support, defaults to yes
  GEN_PDB=<yes or no>          - Generate Program Database (debug symbols for release build)
  DEBUG=<yes or no>            - Debug builds
  MACHINE=<x86 or x64>         - Target architecture (default is x86)

精简配置

nmake /f Makefile.vc mode=static VC=12 RTLIBCFG=static ENABLE_IPV6=no ENABLE_WINSSL=no ENABLE_SSPI=no
posted @ 2017-02-16 17:21  纯白、色  阅读(5795)  评论(0编辑  收藏  举报