我该如何解决这些libcurl链接错误?

9
[Administrator@windows ~]$ g++ client.cpp -lcurl -o client.exe
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0x23): undefined reference to `_imp__curl_global_init'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0x5f): undefined reference to `_imp__curl_formadd'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0x9b): undefined reference to `_imp__curl_formadd'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0xa2): undefined reference to `_imp__curl_easy_init'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0xc8): undefined reference to `_imp__curl_easy_setopt'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0xe4): undefined reference to `_imp__curl_easy_setopt'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0xf1): undefined reference to `_imp__curl_easy_perform'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0x101): undefined reference to `_imp__curl_easy_cleanup'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0x10e): undefined reference to `_imp__curl_formfree'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccKXFUtC.o:client.cpp:(.text+0x11b): undefined reference to `_imp__curl_slist_free_all'
collect2: ld returned 1 exit status

我在Linux上没有这个问题,所以不知道为什么在Windows上会发生这种情况。我已经搜索过了,除了邮件列表档案中出现同样的问题和回复“Google一下”,没有找到其他内容。

我正在使用mingw。构建libcurl时确实收到了一些链接器警告,但它们似乎与ssl有关,我不知道这是否很重要,因为它没有出现错误。

*** Warning: linker path does not have real file for library -lssl.
*** I have the capability to make that library automatically link in when
*** you link to this library.  But I can only do this if you have a
*** shared version of the library, which you do not appear to have
*** because I did check the linker path looking for a file starting
*** with libssl and none of the candidates passed a file format test
*** using a file magic. Last file checked: /ssl/lib/libssl.a

*** Warning: linker path does not have real file for library -lcrypto.
*** I have the capability to make that library automatically link in when
*** you link to this library.  But I can only do this if you have a
*** shared version of the library, which you do not appear to have
*** because I did check the linker path looking for a file starting
*** with libcrypto and none of the candidates passed a file format test
*** using a file magic. Last file checked: /ssl/lib/libcrypto.a

*** Warning: linker path does not have real file for library -lz.
*** I have the capability to make that library automatically link in when
*** you link to this library.  But I can only do this if you have a
*** shared version of the library, which you do not appear to have
*** because I did check the linker path looking for a file starting
*** with libz and none of the candidates passed a file format test
*** using a file magic. Last file checked: /mingw/lib//libz.a
*** The inter-library dependencies that have been dropped here will be
*** automatically added whenever a program is linked with this library
*** or is declared to -dlopen it.

*** Since this library must not contain undefined symbols,
*** because either the platform does not support them or
*** it was explicitly requested with -no-undefined,
*** libtool will only create a static version of it.
4个回答

9
我能够通过添加选项-lcurl.dll来避免在Windows(mingw win32)上出现这些CURL链接错误。在我的情况下,不需要使用-DCURL_STATICLIB
我的构建在mingw/lib文件夹中有两个libcurl文件:libcurl.a和libcurl.dll.a

1
感谢这个面包屑,解决了我的问题:g++: Code::Blocks > 设置 > 编译器 > 链接器设置 > 添加 > lib\libcurldll.a && lib\libcurl.a - Alexx Roche

7
Libtool只构建了一个静态的libcurl库,而没有动态库。你的头文件正在寻找一个动态的libcurl库。这可能不是libcurl的问题,因为我可以看到头文件中支持__declspec(dllimport)和__declspec(dllexport)的代码(这是包作者知道情况的好迹象)。
技术细节:请参见有关libssh的此答案
解决方案:使用-DCURL_STATICLIB编译。

哦,我以为libcurl既可以构建静态库也可以构建动态库。 我尝试了-DCURL_STATICLIB,但现在我收到了数百个链接器错误,说未定义WSAStartup @ 8,_imp__ares_library_init,ntohs @ 4,getsockopt @ 20等。 - VVV
1
你没有链接任何依赖库。你需要(至少)-lwinsock2加上其他库的“-l”标志(看起来像是OpenSSL和可能还有一两个)。 - Jack Kelly

1

我在使用NetBeans 7.1和Mingw时遇到了同样的问题。从属性中,添加库libcurl.dll.a解决了我的问题。

这个文件在我运行mingw make后位于curl-7.28.1\lib.libs下。


0

我在不同的项目中遇到了类似的错误(与libz和libsqlite有关)。这是由GNU libtool脚本产生的。

在我的情况下,原因是缺少这些库的一些文件(.la?)或者可能是库的libz.dll.a变体。

为了在automake/autoconf构建./configure --prefix=... ; make时拥有所有必要的文件,您将需要使用configuremake在相同的MSYS下构建zlibcryptossl。cmake或自定义makefile构建通常无法作为共享库autotool构建的依赖项。

另一个最简单的选择是使用cmake构建动态curl(https://github.com/bagder/curl.git)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接