博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vc2008构建和使用libcurl静态库
阅读量:7071 次
发布时间:2019-06-28

本文共 8528 字,大约阅读时间需要 28 分钟。

1>下载CURL源代码

 2>用VC2008/2005打开工程curl-7.26.0\lib\libcurl.vcproj,转换下工程并构建,可以直接编译成功!

3>新建个控制台工程测试下刚才编译的静态库libcurl.lib,可以在libcurl\curl-7.26.0\docs\examples目录找个简单的使用curl的例子,在这个工程选项Configuration Properties-| C/C++ -|General -|Additional Include Directories 路径中加入curl7.26\include, 在linker选项卡,指定静态库路径和静态库的名字libcurl.lib,代码如下

 

[cpp] 
 
  1. #include "stdafx.h"  
  2. #include <Windows.h>  
  3. #include "curl/curl.h"  
  4.   
  5. int _tmain(int argc, _TCHAR* argv[])  
  6. {  
  7.     CURL *curl;  
  8.     CURLcode res;  
  9.   
  10.     curl = curl_easy_init();  
  11.     if(curl) {  
  12.         curl_easy_setopt(curl, CURLOPT_URL, "http://2345.com/?kduba");  
  13.         res = curl_easy_perform(curl);  
  14.         curl_easy_cleanup(curl);  
  15.     }  
  16.     return 0;  
  17. }  

 

 此时cpp文件可以编译,但是链接报错

1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_init

1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_setopt
1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_perform
1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_cleanup

 

看样子根本没有链接静态库,虽然刚才指定了库的路径,确认库路径的名字没错,于是看了下curl_easy_init 这个函数的定义,

 

[cpp] 
 
  1. CURL_EXTERN CURL *curl_easy_init(void);  
  2. CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);  
  3. CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);  
  4. CURL_EXTERN void curl_easy_cleanup(CURL *curl);  
  5.   
  6.   
  7. /* 
  8.  * Decorate exportable functions for Win32 and Symbian OS DLL linking. 
  9.  * This avoids using a .def file for building libcurl.dll. 
  10.  */  
  11. #if (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__)) && \  
  12.      !defined(CURL_STATICLIB)  
  13. #if defined(BUILDING_LIBCURL)  
  14. #define CURL_EXTERN  __declspec(dllexport)  
  15. #else  
  16. #define CURL_EXTERN  __declspec(dllimport)  
  17. #endif  
  18. #else  

 

 看到这里于是明白了,如下操作:

在libcurl静态库工程选项Configuration Properties-| C/C++ -| Preprocessor 中加上BUILDING_LIBCURL宏

在测试工程选项Configuration Properties-| C/C++ -| Preprocessor 中加上CURL_STATICLIB宏,然后依次重新构建两个工程

发现测试工程链接不过

1>libcurl_MT.lib(easy.obj) : error LNK2001: unresolved external symbol 

1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(easy.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(asyn-thread.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(transfer.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_unbind_s
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_msgfree
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ber_free
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_memfree
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_value_free_len
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_get_values_len
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_next_attribute
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_first_attribute
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_get_dn
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_next_entry
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_first_entry
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_search_s
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_simple_bind_s
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_init
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_set_option
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_err2string
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib() : error LNK2001: unresolved external symbol
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(nonblock.obj) : error LNK2001: unresolved external symbol 
1>libcurl_MT.lib(curl_gethostname.obj) : error LNK2001: unresolved external symbol

谷歌了下, WSACleanup function msdn  是需要链接Ws2_32.lib,

同样的道理

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_unbind_s

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_msgfree
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ber_free
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_memfree
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_value_free_len
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_get_values_len
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_next_attribute
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_first_attribute
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_get_dn
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_next_entry
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_first_entry
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_search_s
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_simple_bind_s
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_init
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_set_option
1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_err2string

是少了Wldap32.lib

在libcurl静态库工程选项Configuration Properties-|Librarian -| Additional Dependencies 中加上依赖项Ws2_32.lib Wldap32.lib

再依次重编两个工程,就OK了

编译选项设为/MD时候,不需要添加Ws2_32.lib Wldap32.lib 

 

小结:

1>对于开源代码的编译问题,还是要从代码入手,包括注释

 2>静态库构建的时候很容易,但是要知道是不是成功的,还得编个测试工程才能知道是不是真的OK

转载地址:http://klzml.baihongyu.com/

你可能感兴趣的文章
解决用sudo执行命令时报"xxx is not in the sudoers file.This incident will be reported"错误
查看>>
代写CSE214作业、代做Java程序作业、代写Java编程作业、Java语言作业代做、代写Java实验作业...
查看>>
Windows Server 2003安装方法
查看>>
学习方向
查看>>
K倍区间
查看>>
POJ 1151 离散化线段树+计算几何正矩形求并
查看>>
数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器...
查看>>
程序员决对不能缺少产品思维
查看>>
photoshop 前端常用技巧
查看>>
递归算法
查看>>
包装类和基本类型区别?(integer和int取值范围一样大)
查看>>
HDU 2896 病毒侵袭 (AC自动机)
查看>>
Python—内置函数
查看>>
错误解决记录-------------验证启动HDFS时遇到的错误
查看>>
java基础类和对象-题
查看>>
2018上海大都会邀请赛J(数位DP)
查看>>
:question.sync=”questionText”父子组件双向绑定
查看>>
jquery动画切换引擎插件 Velocity.js 学习02
查看>>
[Soot学习笔记][5]Soot依赖的两个框架
查看>>
[导入]构筑在GPRS之上的WAP应用
查看>>