将静态 libcurl 添加到 Code::Blocks IDE
我不知道如何正确地将静态 libcurl 库添加到我的 Code::Blocks IDE 中。我希望它是静态的,因为这样在程序运行时就不需要 .dll 文件(默认情况下 Windows 中不包含这些文件)。我正在使用这个 libcurl: http://curl.haxx.se/dlwiz/?type =lib&os=Win32&flav=- (不带 OpenSSL 的 minGW)
以下是我的全局编译器设置:http://img845.imageshack.us/img845/1381/halpr.jpg
我是出现以下错误:
ld.exe||找不到-lCURL_STATICLIB| ||=== 构建完成:1 个错误,0 个警告 ===|
编译此代码时:
include <stdio.h>
include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
显然它没有找到 CURL_STATICLIB,因此我不知道为什么。我什至不确定是否需要将 CURL_STATICLIB 添加到我的链接器设置中(我在其他论坛上阅读过)。我发现有些人有同样的问题,但在任何地方都没有正确回答:
stackoverflow.com/questions/4176503/frusterated-with-libcurl
forums.codeblocks.org/index.php?topic=11391.0
old.nabble.com/gcc-working-with-libcurl-td20506927.html
forums.devshed.com/c-programming-42/linker-error-using-libcurl-698071.html
我是厌倦了与此斗争,请帮助我。
编辑:
你好维克多,谢谢你的回复!
我会尽力尽可能详细,以免造成误解。 因此,这是我的 C:\libs\ 文件夹的目录/文件夹树的图像:
http://img199.imageshack.us/img199/6977/curl1.png
正如你所看到的,它还包括构建日志,你会注意到这个错误时间是 与我之前发布的不同。这是因为我更改了全局编译器和构建项目设置。
我的新构建项目设置: http://img863.imageshack.us/img863/4404/buildoptions .png 我的新全局编译器设置: http://img225.imageshack.us/img225/4926/curl2 .png
我确信我配置了这些设置错误,这就是我无法编译它的原因。
I can't figure out how to properly add a static libcurl library to my Code::Blocks IDE. I want it static because then no .dll files, which are not included in Windows by default, are needed during runtime of my program. I am using this libcurl:
http://curl.haxx.se/dlwiz/?type=lib&os=Win32&flav=- (minGW without OpenSSL)
Here are my global compiler settings: http://img845.imageshack.us/img845/1381/halpr.jpg
I am getting the following error:
ld.exe||cannot find -lCURL_STATICLIB| ||=== Build finished: 1 errors, 0 warnings ===|
when compiling this code:
include <stdio.h>
include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Obviously it does not find CURL_STATICLIB, thu I got no idea why. I am not even sure if it was needed to add CURL_STATICLIB to my linker settings(I read it on other forums). I found some guys having same problem, but it isn't properly answered on any place:
stackoverflow.com/questions/4176503/frustrated-with-libcurl
forums.codeblocks.org/index.php?topic=11391.0
old.nabble.com/gcc-working-with-libcurl-td20506927.html
forums.devshed.com/c-programming-42/linker-error-using-libcurl-698071.html
I am so tired of fighting with this, please help me.
EDIT:
Hello Victor, thank you for response!
I will try to be as detailed as possible, so there are no missunderstandings.
So, here is the image of the directory/folder tree for my C:\libs\ folder:
http://img199.imageshack.us/img199/6977/curl1.png
As you can see, it also includes build log, you will notice that the error this time is
different than the one I posted previously. It's because I changed global compiler and build project settings.
My new Build Project settings: http://img863.imageshack.us/img863/4404/buildoptions.png
My new Global Compiler settings: http://img225.imageshack.us/img225/4926/curl2.png
I am sure I have configured these settings wrong and that's why I can not compile it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我成功地使用静态链接使用 libcurl 构建了您的示例。完成这项工作所涉及的细节非常复杂——对于粗心的人来说,正确设置它可能会变得很棘手。
以下是我用于完成这项工作的步骤,请务必仔细遵循它们:
转到项目构建选项->编译器设置->#defines:输入
CURL_STATICLIB
。定义后,libcurl.h 标头将对其函数签名进行预处理以适应静态链接。否则,假定动态链接,并且损坏的名称将变为_imp__*
。屏幕截图中未解决的错误表明它正在尝试动态链接而不是所需的静态链接。在项目构建选项->链接器设置->链接库下确保它包含以下内容:curl、rtmp、idn、ssl、ssh2、crypto、z、ws2_32、wldap32、winmm、gdi32。请注意,顺序很重要。由于 gnu 链接器的设计缺陷,需要首先列出最依赖的库,然后列出最不依赖的库。其他链接器(例如 msvc link 和 borland 的 ilinker)不会出现此类问题 - 库可以按任何顺序列出。
在项目构建选项->链接器设置->其他链接器选项下添加“-static”。这将确保使用“idn”的静态版本。如果省略此开关,那么您编译的程序可能依赖于“libidn-11.dll”来运行,这可能不是您想要的。
此时,您应该能够毫无问题地编译和链接 libcurl 程序。有几件事值得一提,
在“其他链接器选项”下,不需要屏幕截图中的其他额外开关。 “libcurl.a”已列出并由链接库覆盖。
“libcrypto.a”似乎包含与“libeay32.a”相同的引用,因此只需要其中一个。然而,“libeay32.a”尽管尺寸较大,但仍会导致动态链接。如果您希望您的应用程序“完全独立”,请使用“libcrypto.a”,如屏幕截图中所示。
如果您希望将来动态链接,只需将链接库下的列表替换为“curldll”并删除
CURL_STATICLIB
定义即可。不需要额外的库(例如 ssl、idn、rtmp 等),因为 libcurl.dll 已经涵盖了它们。您可以通过使用代码块的用户模板来避免新 libcurl 程序的繁琐且容易出错的设置。 (例如,文件->新建->项目->用户模板)
希望这可以一劳永逸地解决您使用 libcurl 时遇到的任何构建问题。
Okay, I managed to build your example successfully with libcurl using static linkage. The details involved to make this work are quite intricate -- setting it up correctly can get tricky for the unwary.
Here are the steps I used to make this work, be sure to follow them carefully:
Go to Project build options->Compiler settings->#defines: type in
CURL_STATICLIB
. When this is defined the libcurl.h header will have its function signatures preprocessed to fit static linkage. Otherwise dynamic linkage is assumed and the mangled names then become_imp__*
. The unresolved errors from your screenshot indicate it's attempting a dynamic link rather than the desired static link.Under Project build options->Linker settings->Link libraries make sure it contains the following: curl, rtmp, idn, ssl, ssh2, crypto, z, ws2_32, wldap32, winmm, gdi32. Note that order is important. Due to a design deficiency of the gnu linker, the most dependant libraries need to be listed first followed by least dependant. Other linkers like msvc link and borland's ilinker do not exhibit such issues -- the libraries can be listed in any order.
Under Project build options->Linker settings->Other linker options add in '-static'. This will make sure that the static version of 'idn' is used. If this switch is omitted then your compiled program could depend on 'libidn-11.dll' to run which probably isn't what you want.
At this point, you should be able to compile and link libcurl programs without any issues. A couple things worth mentioning,
Under Other linker options the other extra switches from your screenshot aren't needed. 'libcurl.a' is already listed and covered by Link libraries.
The 'libcrypto.a' seems to cover the same references as the 'libeay32.a' so only one of them is needed. However, 'libeay32.a' causes dynamic linkage despite its larger size. If you want your application to be 'fully self-contained' use 'libcrypto.a' instead like in the screenshot.
If you wish to link dynamically in the future, just replace the listing with 'curldll' under Link libraries and remove the
CURL_STATICLIB
define. The extra libraries (eg. ssl, idn, rtmp etc.) aren't needed since libcurl.dll already covers them.You can avoid the tedious error prone setup of a new libcurl program by employing codeblocks' user templates. (eg. File->New->Project->User templates)
Hopefully this resolves any build problems you have with libcurl once and for all.