在程序中使用libcurl的的静态链接库产生的未导出符号的错误(zt)

 

今天在使用libcurl的时候出现一个很普遍,但是叫我怎么也费了半天劲的错误

unresolved external symbol

我也得代码很简单:

#include <stdio.h>
#include <curl.h>
#pragma comment ( lib, "libcurl.lib" )
void main()
{
CURL*   curl;
CURLcode ret;
//
// 初始化libcurl库
//
curl = curl_easy_init();
if ( curl )
{
   curl_easy_setopt( curl, CURLOPT_URL, "http://www.baidu.com" );
   ret = curl_easy_perform( curl );

   curl_easy_cleanup( curl );
}
else
{
   printf( "initialize the lib error!/n" );
   return;
}
}

但是这个错误一直烦着我,怎么弄也不行,换了好几种方法使用静态链接库,都不行.

google了半个小时,发现有不少人都是出现这个问题

见:http://forums.devshed.com/c-programming-42/c-linking-problem-567669.html

这两个哥们还真是有想象力,唉不过他们想到的我也已经试过了,晕..

最后还是再他老家,找到了答案:

When building an application that uses the static libcurl library, you must
add -DCURL_STATICLIB to your CFLAGS. Otherwise the linker will look for
dynamic import symbols. If you get linker error like "unknown symbol
__imp__curl_easy_init ..." you have linked against the wrong (static)
library. If you want to use the libcurl.dll and import lib, you don't need
any extra CFLAGS, but use one of the import libraries below

在这里:http://curl.haxx.se/docs/faq.html#Link_errors_when_building_libcur

所以我们这样来改

菜单-->project -->settings-->C++选项卡的General里面的Project Option里面加上-DCURL_STATICLIB (其实就相当于在Preprocessor definitions里面加上CURL_STATICLIB一样)

确定-->rebuild all

又出错了:

libcurl.lib(easy.obj) : error LNK2001: unresolved external symbol __imp__WSACleanup@0
libcurl.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSACleanup@0
libcurl.lib(easy.obj) : error LNK2001: unresolved external symbol __imp__WSAStartup@8

.....

这个一看就明白了马上加上:

#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "wldap32.lib" )

编译-->叮的一个美妙的声音想起了,哈哈,编译成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值