将E:\01_code\tools\curl-7.54.0\build\Win32\VC12\LIB Release - LIB OpenSSL - LIB LibSSH2目录下的编译生成的libcurl.lib拷贝curl目录中
将E:\01_code\tools\libssh2-1.8.0-vc11-x86\lib\libssh2.lib拷贝到curl目录中
将E:\01_code\tools\openssl-1.0.2k-vc11-x86\lib目录下libeay32.lib、ssleay32.lib拷贝到curl目录中
将所有的.h文件添加到VS项目中
一、添加libcurl.dll和其它相关库到工程
1、添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录。

2、添加文件引用的lib静态库路径:工程---属性---配置属性---链接器---常规---附加库目录:加上lib文件存放目录。
然后添加工程引用的lib文件名:工程---属性---配置属性---链接器---输入---附加依赖项:加上lib文件名。


3、添加工程引用的dll动态库:把引用的dll放到工程的可执行文件所在的目录下
使用静态库,这部可忽略
二、编译过程中错误解决
1.
1>curlTest.obj : error LNK2019: 无法解析的外部符号 __imp__curl_global_init,该符号在函数 _main 中被引用
1>curlTest.obj : error LNK2019: 无法解析的外部符号 __imp__curl_global_cleanup,该符号在函数 _main 中被引用
1>curlTest.obj : error LNK2019: 无法解析的外部符号 __imp__curl_easy_init,该符号在函数 "int __cdecl curl_test(void)" (?curl_test@@YAHXZ) 中被引用
1>curlTest.obj : error LNK2019: 无法解析的外部符号 __imp__curl_easy_setopt,该符号在函数 "int __cdecl curl_test(void)" (?curl_test@@YAHXZ) 中被引用
1>curlTest.obj : error LNK2019: 无法解析的外部符号 __imp__curl_easy_perform,该符号在函数 "int __cdecl curl_test(void)" (?curl_test@@YAHXZ) 中被引用
1>curlTest.obj : error LNK2019: 无法解析的外部符号 __imp__curl_easy_cleanup,该符号在函数 "int __cdecl curl_test(void)" (?curl_test@@YAHXZ) 中被引用
错误原因:编译的libcurl.lib是release版本,WinLibcurlTest编译的是debug版本,版本不一置。
2.
1>LINK : fatal error LNK1104: 无法打开文件“libcurl.lib”
1、给工程添加依赖的库:项目->属性->链接器->输入->附加依赖项,把libcurl.lib、ws2_32.lib、winmm.lib、wldap32.lib添加进去

2、加入预编译选项:项目->属性->c/c++ ->预处理器->预处理器定义,添加BUILDING_LIBCURL

再次编译,报如下错误:
1>libcurl.lib(easy.obj) : error LNK2001: 无法解析的外部符号 _libssh2_init
1>libcurl.lib(easy.obj) : error LNK2001: 无法解析的外部符号 _libssh2_exit
1>libcurl.lib(version.obj) : error LNK2001: 无法解析的外部符号 _libssh2_version
给工程添加依赖的库:项目->属性->链接器->输入->附加依赖项,把libssh2.lib添加进去
再次编译,报如下错误:
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_write
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_ctrl
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_CTX_ctrl
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_get_error
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSLv3_client_method
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSLv23_client_method
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_shutdown
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_set_connect_state
1>libcurl.lib(openssl.obj) : error LNK2001: 无法解析的外部符号 _SSL_library_init
给工程添加依赖的库:项目->属性->链接器->输入->附加依赖项,把libeay32.lib、ssleay32.lib添加进去
再次编译,成功。
最终的附加依赖项如下:

三、测试
运行WinLibcurlTest.exe,弹出如下错误:

将E:\01_code\tools\libssh2-1.8.0-vc11-x86\bin目录下libssh2.dll拷贝到WinLibcurlTest.exe所在目录下,再次运行WinLibcurlTest.exe,报如下错误:

将E:\01_code\tools\openssl-1.0.2k-vc11-x86\bin目录下libeay32.dll拷贝到WinLibcurlTest.exe所在目录下,再次运行WinLibcurlTest.exe,报如下错误:

将E:\01_code\tools\openssl-1.0.2k-vc11-x86\bin目录下ssleay32.dll拷贝到WinLibcurlTest.exe所在目录下,再次运行WinLibcurlTest.exe
程序运行成功:

测试代码如下:
-
#include <stdio.h>
-
#include <string.h>
-
#include "curl/curl.h"
-
-
// 添加依赖库
-
#pragma comment(lib, "libcurl.lib")
-
#pragma comment(lib, "ws2_32.lib")
-
#pragma comment(lib, "winmm.lib")
-
#pragma comment(lib, "wldap32.lib")
-
-
-
size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
-
{
-
static int current_index = 0;
-
current_index++;
-
printf("current: %d\n", current_index);
-
printf("%s\n", (char*)buffer);
-
int temp = *(int*)userp;
-
return nmemb;
-
}
-
-
int curl_test(void)
-
{
-
CURL* curl = curl_easy_init();
-
if (NULL == curl)
-
{
-
printf("(curl is NULL!/r/n");
-
return -1;
-
}
-
-
int my_param = 1;
-
// 设置目标URL
-
curl_easy_setopt(curl, CURLOPT_URL, "");
-
// 设置接收到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;
-
}
-
-
int main()
-
{
-
curl_global_init(CURL_GLOBAL_ALL);
-
curl_test();
-
curl_global_cleanup();
-
getchar();
-
return 0;
- }