vc2008构建和使用libcurl静态库

http://blog.csdn.net/mos2046/article/details/7697530

1>下载CURL源代码curl-7.26.0.zip

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

此时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] view plaincopy
  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. * Decorate exportable functions for Win32 and Symbian OS DLL linking.
  7. * This avoids using a .def file for building libcurl.dll.
  8. */
  9. #if (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__)) && \
  10. !defined(CURL_STATICLIB)
  11. #if defined(BUILDING_LIBCURL)
  12. #define CURL_EXTERN  __declspec(dllexport)
  13. #else
  14. #define CURL_EXTERN  __declspec(dllimport)
  15. #endif
  16. #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 __imp__WSACleanup@0
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSACleanup@0
1>libcurl_MT.lib(easy.obj) : error LNK2001: unresolved external symbol __imp__WSAStartup@8
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSAStartup@8
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__WSAGetLastError@0
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(asyn-thread.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(transfer.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__send@16
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__send@16
1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__recv@16
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__recv@16
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__getsockname@12
1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__getsockname@12
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__getpeername@12
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__ntohs@4
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__ntohs@4
1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__ntohs@4
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__WSASetLastError@4
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__WSASetLastError@4
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol __imp__WSASetLastError@4
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__getsockopt@20
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__setsockopt@20
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__connect@12
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__bind@12
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__bind@12
1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__bind@12
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__htons@4
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__htons@4
1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__htons@4
1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__htons@4
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__closesocket@4
1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__socket@12
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__freeaddrinfo@4
1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__getaddrinfo@16
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__sendto@24
1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__recvfrom@24
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(ftp.obj) : error LNK2001: unresolved external symbol__imp__listen@8
1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__accept@12
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol ___WSAFDIsSet@8
1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol __imp__select@20
1>libcurl_MT.lib(nonblock.obj) : error LNK2001: unresolved external symbol __imp__ioctlsocket@12
1>libcurl_MT.lib(curl_gethostname.obj) : error LNK2001: unresolved external symbol__imp__gethostname@8

谷歌了下, 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

感谢网友指正:

Ws2_32.lib Wldap32.lib 加到测试工程就行了,不用加到libcurl工程中

小结:

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

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

http://bbs.csdn.net/topics/390312205

原来__declspec(dllexport)与__declspec(dllimport)声明只应用于动态链接,不能应用于静态链接,以前都没注意这个问题

CURL_STATICLIB 作用于
#if (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__)) && \
     !defined(CURL_STATICLIB)
#if defined(BUILDING_LIBCURL)
#define CURL_EXTERN  __declspec(dllexport)
#else
#define CURL_EXTERN  __declspec(dllimport)
#endif
#else

CURL_EXTERN 作用于
CURL_EXTERN CURL *curl_easy_init(void);

编译工程去除 lib libcurld.lib ws2_32.lib wldap32.lib
定义 CURL_STATICLIB 编译结果
curl_test.obj : error LNK2019: 无法解析的外部符号 _curl_easy_init,该符号在函数 _main 中被引用

不定义CURL_STATICLIB 编译结果
curl_test.obj : error LNK2019: 无法解析的外部符号 __imp__curl_easy_init,该符号在函数 _main 中被引用

libcurl.lib 导入问题 须知相关推荐

  1. Qt,编译libcurl并且导入到库

    在MInGW环境下编译和使用libcurl静态库 libcurl 7.21以后的版本在Windows下的编译比较简单,自带了MinGW和VC环境的Makefile文件,首先去Curl官网下载源代码:h ...

  2. LINK : fatal error LNK1181: cannot open input file “libcurl.lib“

    接手技术顾问的一个老项目,VC6.0,编译项目,提示: LINK : fatal error LNK1181: cannot open input file "libcurl.lib&quo ...

  3. android studio 导入module作为lib使用

    android studio 导入module作为lib使用 1.将 android module导入 android project 中 2.在要作为lib导入的module 的build.grad ...

  4. LIBCURL上传文件, 下载文件,打开网页,POST数据,支持https等一系列函数方便各种模拟登陆注册账户

    以前写过类似代码,后来电脑硬盘坏了现在又从新写过. 主要是为了模拟登陆或者注册账户使用.本代码是在VS2015 多字符集环境下写的. 一些代码是网上找的现成的自己加工修改而来,贡献出来大家参考. CU ...

  5. lib和dll文件的区别和联系(集合了几个博客的内容)

    lib和dll lib和dll文件的区别和联系 .dll是在你的程序运行的时候才连接的文件,因此它是一种比较小的可执行文件格式,.dll还有其他的文件格式如.ocx等,所有的.dll文件都是可执行. ...

  6. linux libcurl 库使用方法

    一.ibcurl简介 作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, I ...

  7. 动态链接库dll,静态链接库lib, 导入库lib

    目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称"静态库"),另一种为动态连接库(DLL,以下简称"动态库")的导入库(Imp ...

  8. vs显式导入(代码注入)依赖库

    #pragma comment(lib,"libcurl/lib/libcurl_debug.lib")   //加载依赖库 #pragma comment (lib, " ...

  9. 动态链接库dll,静态链接库lib, 导入库lib 转

    动态链接库dll,静态链接库lib, 导入库lib 在用VS编译工程的时候,我们会选择动态链接库dll,静态链接库lib(static library),可是为什么在编译动态链接库的时候也可以指定输出 ...

最新文章

  1. 替换某个字符串_postman教程-10-如何在集合中快速查询和替换数据
  2. 1.2W 字的 SQL 语法速成手册
  3. Android知识点小结
  4. Golang 入门系列(十) mysql数据库的使用
  5. Linux IPv6 UDP套接字编程示例
  6. VTK:Rendering之TransparentBackground
  7. php查看表创建结构,MySQL 查看表结构简单命令
  8. ELK+Filebeat+Kafka+ZooKeeper 构建海量日志分析平台(elk5.2+filebeat2.11)
  9. shell中的大括号和小括号
  10. 秩和比RSR法算法C语言,秩和比法
  11. 手持普通计算机怎么开根号,数学里怎么开根号?具体方法
  12. 超详细域名和二级域名、子域名免费配置SSL证书变成升级HTTPS(完整配置文件)
  13. C#中汉字按照首字拼音排序
  14. 【史上最全 | 编程入门指南无标题】
  15. 量化7年1000万知与行,回测量化7年如果只每月定投5.4万到沪深300指数基金,能否实现7年1000万
  16. 股票预测和股票分析就用FineBI!
  17. 【转】PCM Audio,PCM格式音频文件 详解
  18. 再高贵的打工人都得在体检报告前低下高贵的头颅
  19. guido正式对外发布python版本的年份_Guido van Rossum正式对外发布Python版本的年份是:______。...
  20. 【学习OpenCV4】图像金字塔总结

热门文章

  1. 2.15 Spring Framework 5.x 之ApplicationContext附加功能
  2. android多媒体备忘录,基于android的多媒体备忘录的设计与实现
  3. python-爬虫,实现输入关键字,然后爬取关键字主页代码并存储到mysql数据库
  4. 多线程-线程让步(yield)
  5. 传统算法与神经网络算法,网络神经算法的应用
  6. 2023兔年凯立德车载导航最新版
  7. 【Industry digitization】数字化技术正在全球范围内迅猛发展,世界各国和企业纷纷开启数字化转型之路
  8. 使用html5的游戏,使用HTML5制作游戏
  9. 从GPU编程到SIMT核心
  10. linux 制作iso 和 刻录DVD