将静态libcurl添加到Code :: Blocks IDE [英] Adding static libcurl to Code::Blocks IDE

查看:916
本文介绍了将静态libcurl添加到Code :: Blocks IDE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何正确地添加一个静态libcurl库到我的Code :: Blocks IDE。我想让它静态,因为没有.dll文件,这是默认情况下不包括在Windows中,在我的程序运行时需要。我正在使用此libcurl:
http://curl.haxx.se / dlwiz /?type = lib& os = Win32& flav = - (没有OpenSSL的minGW)

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)

这是我的全局编译器设置:http://img845.imageshack.us/img845/1381/halpr.jpg

Here are my global compiler settings: http://img845.imageshack.us/img845/1381/halpr.jpg

我得到以下错误:


ld.exe ||找不到-lCURL_STATICLIB | || === Build finished:1 errors,0 warnings === |

ld.exe||cannot find -lCURL_STATICLIB| ||=== Build finished: 1 errors, 0 warnings ===|

编译此代码时:

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到我的链接器设置(我在其他论坛上阅读)。我发现一些人有同样的问题,但它没有在任何地方得到正确回答:

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

stackoverflow.com/questions/4176503/frustrated-with-libcurl

forums.codeblocks.org/index.php?topic=11391.0

forums.codeblocks.org/index.php?topic=11391.0

old.nabble .com / gcc-working-with-libcurl-td20506927.html

old.nabble.com/gcc-working-with-libcurl-td20506927.html

forums.devshed.com/c-programming-42/linker-error-using-libcurl-698071 .html

forums.devshed.com/c-programming-42/linker-error-using-libcurl-698071.html

我很累了,请帮我。

编辑:

Hello Victor,谢谢您的回应!

Hello Victor, thank you for response!

尽可能地,所以没有误解。
所以,这里是我的C:\libs \文件夹的目录/文件夹树的图像:

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.

我的新项目设置:http://img863.imageshack.us/img863/4404/buildoptions.png
我的新的全局编译器设置: http://img225.imageshack.us/img225/4926/curl2.png

我相信

推荐答案

好吧,我设法建立你的例子成功与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:


  1. 转到项目构建选项 - >编译器设置 - >#define :键入 CURL_STATICLIB 。当这被定义时,libcurl.h头将使其函数签名被预处理以适合静态链接。否则,假定动态链接,并且被标记的名称变为 _imp __ * 。您屏幕截图中未解析的错误表示正在尝试动态链接,而不是所需的静态链接。

  1. 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.

在项目构建选项 - >链接器设置 - >链接库下,确保其包含以下内容: curl,rtmp,idn,ssl,ssh2,crypto,z,ws2_32,wldap32,winmm,gdi32 。注意顺序很重要。由于gnu接头的设计缺陷,最依赖的文库需要首先列出,然后最少依赖。

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.

项目构建选项下, >链接器设置 - >其他链接器选项中添加-static。这将确保使用静态版本的'idn'。如果省略这个开关,那么你编译的程序可能依赖于'libidn-11.dll'运行,这可能不是你想要的。

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.

此时,您应该能够编译和链接libcurl程序没有任何问题。值得一提的有几件事,

At this point, you should be able to compile and link libcurl programs without any issues. A couple things worth mentioning,


  • 其他连结选项不需要。 'libcurl.a'已列出并由链接库覆盖。

  • Under Other linker options the other extra switches from your screenshot aren't needed. 'libcurl.a' is already listed and covered by Link libraries.

'libcrypto.a'引用作为'libeay32.a',因此只需要其中的一个。然而,'libeay32.a'导致动态链接,尽管其更大的大小。

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.

如果您希望动态链接,请使用libcrypto.a未来,只需在链接库下用curldll替换列表,并删除 CURL_STATICLIB define。

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.

您可以避免繁琐的错误通过使用代码块的用户模板来容易地设置新的libcurl程序。 (例如 File-> New-> Project-> User templates

You can avoid the tedious error prone setup of a new libcurl program by employing codeblocks' user templates. (eg. File->New->Project->User templates)

这解决了所有与libcurl一次和所有的生成问题。

Hopefully this resolves any build problems you have with libcurl once and for all.

这篇关于将静态libcurl添加到Code :: Blocks IDE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆