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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5485081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:31:13  来源:igfitidea点击:

How do I solve these libcurl linking errors?

c++windowscurllinkermingw

提问by VVV

[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

I don't have this problem on linux so I don't know why this happens on windows. I googled it already and didn't find anything except mailing list archives with the same question and reply saying "google it".

我在 linux 上没有这个问题,所以我不知道为什么在 windows 上会发生这种情况。我已经用谷歌搜索了它,除了带有相同问题的邮件列表档案之外没有找到任何东西,并回复说“谷歌它”。

I'm using mingw. I did get some linker warnings when I built libcurl but they seemed to be ssl related and I don't know if it's a big deal because it built without errors.

我正在使用 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.

回答by mstrthealias

I was able to avoid these curl linking errors on windows (mingw win32) by adding option -lcurl.dll. -DCURL_STATICLIBwas not needed in my case.

通过添加 option ,我能够在 Windows (mingw win32) 上避免这些 curl 链接错误-lcurl.dll-DCURL_STATICLIB在我的情况下不需要。

My build has two libcurl files in mingw/lib folder: libcurl.a and libcurl.dll.a

我的构建在 mingw/lib 文件夹中有两个 libcurl 文件: libcurl.a and libcurl.dll.a

回答by Hyman Kelly

Libtool only built a static libcurl and not a dynamic library. Your headers are looking for a dynamic libcurl. It's probably not libcurl's fault, because I can see code in the headers that supports __declspec(dllimport)and __declspec(dllexport)(that's a good sign the package author knows what's what.

Libtool 只构建了静态 libcurl 而不是动态库。您的标头正在寻找动态 libcurl。这可能不是 libcurl 的错,因为我可以在头文件中看到支持__declspec(dllimport)和的代码__declspec(dllexport)(这是一个好兆头,包作者知道什么是什么。

Technical details: see this answer regarding libssh.

技术细节:请参阅有关 libssh 的此答案

Solution: Compile with -DCURL_STATICLIB.

解决方法:编译-DCURL_STATICLIB

回答by Gorky

Was having the same issue using netbeans 7.1 with mingw. From properties, linker adding library libcurl.dll.a solved the issue for me.

在 mingw 中使用 netbeans 7.1 时遇到了同样的问题。从属性来看,链接器添加库 libcurl.dll.a 为我解决了这个问题。

This file was located under curl-7.28.1\lib.libs after I ran the mingw make.

运行 mingw make 后,该文件位于 curl-7.28.1\lib.libs 下。

回答by akks

I had similar error (with libz and libsqlite) in different projects. It is produced by GNU libtool script.

我在不同的项目中遇到了类似的错误(使用 libz 和 libsqlite)。它由 GNU libtool 脚本生成。

The reason in my case was lack of some files for these libraries (.la ?) or maybe libz.dll.a variants of the libraries.

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

To have all necessary files for automake/autoconf build ./configure --prefix=... ; make, you'll have to build zlib, cryptoand sslwith configureand maketo under the same MSYS. cmake or custom makefile builds will usually not work as dependencies for shared-library autotool build.

要拥有的automake / autoconf的构建所有必要的文件./configure --prefix=... ; make,你就必须建立zlibcryptosslconfiguremake相同的MSYS下。cmake 或自定义 makefile 构建通常不会作为共享库自动工具构建的依赖项工作。

Another and the most simple option is to build dynamic curl with cmake ( https://github.com/bagder/curl.git)

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