历经千辛万苦,终于把libcurl配置好了
环境:win10 64位
版本:vs2017 curl 7.60.0
1.curl下载地址
https://curl.haxx.se/download.html
2.编译 要在64位上生成32位的lib
(1)在“命令控制”窗口,使用vcvarsall.bat配置环境,
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsamd64_x86.bat
参考链接:https://blog.csdn.net/soaringlee_fighting/article/details/78043905
(2)进入curl 7.60.0目录下的winbuild文件夹下:
输入:nmake /f Makefile.vc mode=static VC=14 MACHINE=x86 DEBUG=yes
如果想使用动态编译,将mode=static改为mode=dll。(本文仅教静态编译,同时curl官方也不建议使用动态编译)
如果使用x64,将MACHINE=x86改为MACHINE=x64。
如果需要release版,将DEBUG=yes改为DEBUG=no。
若正常生成,得到以下结果,可进行第三步,配置工程,进行验证。保留名字最短的文件夹,其他可以删除。
若遇到问题:
libcurld.lib(idn_win32.obj) : error LNK2001: unresolved external symbol __imp__IdnToAscii@20 libcurld.lib(idn_win32.obj) : error LNK2001: unresolved external symbol __imp__IdnToUnicode@20
参考链接,修改源码:https://www.cnblogs.com/passedbylove/p/5979927.html,再重新编译。
(3)配置工程
项目——属性——VC++目录——包含目录:...\curl-7.60.0\builds\libcurl-vc14-x86-debug-static-ipv6-sspi-winssl\include
项目——属性——VC++目录——库目录:...\E:\curl-7.60.0\builds\libcurl-vc14-x86-debug-static-ipv6-sspi-winssl\lib
项目——属性——C/C++——预处理器:
BUILDING_LIBCURL
HTTP_ONLY
CURL_STATICLIB
项目——属性——链接器——输入:
libcurl_a_debug.lib
ws2_32.lib
winmm.lib
wldap32.lib
Crypt32.lib
注意:
测试代码:
#include "pch.h"
#include <iostream>
#include <curl/curl.h>
int main()
{
//curl_easy_init();
CURL *curl = nullptr;
CURLcode res;
curl = curl_easy_init();
if (curl != nullptr) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
成功:
以上为debug x86的配置方法,若配置release版本的,需编译libcurl release版本,加入对应的包含目录、库目录以及对应的lib文件,其他一致。