问题描述
我不知道如何将静态 libcurl 库正确添加到我的 Code::Blocks IDE.我希望它是静态的,因为在我的程序运行期间不需要 Windows 中默认不包含的 .dll 文件.我正在使用这个 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|||==== 构建完成:1 个错误,0 个警告 ===|
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
我已经厌烦了,请帮帮我.
I am so tired of fighting with this, please help me.
您好维克多,感谢您的回复!
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
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.
推荐答案
好的,我使用静态链接成功地使用 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:
转到项目构建选项->编译器设置->#defines:输入
CURL_STATICLIB
.定义后,libcurl.h 标头将对其函数签名进行预处理以适应静态链接.否则,假定动态链接,并且损坏的名称将变为_imp__*
.屏幕截图中未解决的错误表明它正在尝试动态链接而不是所需的静态链接.
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 链接器的设计缺陷,需要首先列出最依赖的库,然后是最不依赖的库.msvc link 和 borland 的 ilinker 等其他链接器不会出现此类问题——这些库可以按任何顺序列出.
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"尽管尺寸较大,但会导致动态链接.如果您希望您的应用程序完全自包含",请使用libcrypto.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.
如果您希望将来动态链接,只需在链接库下用'curldll'替换列表并删除CURL_STATICLIB
定义.不需要额外的库(例如 ssl、idn、rtmp 等),因为 libcurl.dll 已经涵盖了它们.
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 程序的繁琐且容易出错的设置.(例如,文件->新建->项目->用户模板)
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!