libcurl库在VS2013(或更高)环境下的编译,环境配置,demo测试,错误处理

环境及工具

vs2013professional,
win10 x64
curl-7.65.3:可以官网下载:https://curl.haxx.se/download.html
也可以从
https://download.csdn.net/download/birenxiaofeigg/11434185 下载(已验证)
在这里插入图片描述

curl编译(此为网上多数编译方式,但有问题,下面有解决方法):

libcurl库是不能直接使用的,必须经过编译后,生成 .lib/.dll库后,才能使用。
若不想编译,也可以直接下载我编译好的文件包:
https://download.csdn.net/download/birenxiaofeigg/11434198

curl-7.65.3.zip解压后,打开文件夹:

1.双击buildconf.bat

2.打开VS2013命令提示:

在这里插入图片描述
在这里插入图片描述

3.在命令窗口打开curl-7.65.3/winbuild文件夹

编译debug版本:
输入nmake 指令:nmake /f Makefile.vc mode=static VS=12 MACHINE=x64 DEBUG=yes
在这里插入图片描述
编译release版本:
稍等半分钟,编译结束后,可以顺道把release版也编译一下:
nmake /f Makefile.vc mode=static VS=12 MACHINE=x64 DEBUG=no
在这里插入图片描述

4.得到builds文件夹下编译结果

此时,打开 curl-7.65.3文件夹,再打开builds文件夹,即可以看到有6个文件夹。
选择名字最短的两个:
libcurl-vc12-x64-debug-static-ipv6-sspi-winssl

libcurl-vc12-x64-release-static-ipv6-sspi-winssl
至此,编译结束,可以更改两个文件夹为短点的名字后,copy到其他目录备用。

demo测试:

新建一个c++控制台项目curl_test,备用(默认debug x64模式)

1.环境配置:

a.属性—>配置属性—>VC++目录:
包含目录:增加libcurl-vc12-x64-debug-static-ipv6-sspi-winssl文件夹下的include
比如: F:\MyFiles\curl_library\libcurl-vc12-x64-debug\include;
库目录:增加libcurl-vc12-x64-debug-static-ipv6-sspi-winssl文件夹下的lib
比如:F:\MyFiles\curl_library\libcurl-vc12-x64-debug\lib;
b.属性—>配置属性—>C/C++:
预处理器—>预处理器定义:增加 CURL_STATICLIB
代码生成—>运行库: 多线程调试 DLL (/MDd)
c. 链接器:输入—>libcurl_a.lib;Ws2_32.lib;Wldap32.lib;winmm.lib;Crypt32.lib;Normaliz.lib

2.demo测试

demo可下载:https://download.csdn.net/download/birenxiaofeigg/11434209
curl_test 添加如下代码:

// curl_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <curl/curl.h>

int _tmain(int argc, _TCHAR* argv[])
{
	CURL* curl = 0;
	CURLcode res;
	curl = curl_easy_init();
	if (curl != 0)
	{
		curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");

		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

		res = curl_easy_perform(curl);

		if (res != CURLE_OK)
		{
			fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
		}
	}
	system("pause");
	return 0;
}


3.build错误

在这里插入图片描述
此时出现build错误。
显示: error LNK2019: 无法解析的外部符号 __imp__IdnToAscii@20
。。。等错误;

4.错误解决

原因分析:
在对curl7.63.5文件进行编译时,使用的是静态编译,但nmake静态编译时,并没有把依赖库编译进libcurl_a.lib 而通过nmake返回信息,编译curl.exe时,会link libcurl_a.lib 以及依赖库. 由此可知,使用libcurl静态库时,必须同时使用它的依赖库。

解决方法:
在对curl7.63.5文件进行编译时,使用动态编译:
nmake /f Makefile.vc mode=dll VS=12 MACHINE=x64 ENABLE_IDN=no DEBUG=yes
(注意,加入了ENABLE_IDN=no)
以及
nmake /f Makefile.vc mode=dll VS=12 MACHINE=x64 ENABLE_IDN=no DEBUG=no
(注意,加入了ENABLE_IDN=no)

最后得到的编译结果:
在这里插入图片描述

5.重新测试,再出错误:

如此,重新配置好环境后,再测试:
build时,生成正常,在运行时,出错,找不到 xxx.dll:
在这里插入图片描述

6.解决方法:

把编译后文件夹libcurl-vc12-x64-release-dll-ipv6-sspi-winssl目录下,bin/libcurl_debug.dll
复制到 curl_test同目录的x64/debug文件夹中,注意是,curl_test项目,最外面的x64文件夹下的debug:
在这里插入图片描述
如此,再运行项目,即可顺利运行出来了。

发布了56 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/birenxiaofeigg/article/details/97373997