using libcurl without dll

2020-01-28 08:59发布

问题:

I am using Microsoft Visual C++ 2010, and I need to make an application that does not require the libcurl dll. I am defining CURL_STATICLIB in the preprocessor directives and linking to libcurl.lib, libcurl_static.lib, ws2_32.lib, and winmm.lib, but it still requires the dll to work. If I only link to libcurl_static.lib, it has undefined external symbol errors. How can I get it working?

I have also tried building the source but I get 13 errors (wow, unlucky number) that all say "error C2011: 'pollfd' : 'struct' type redefinition". Could someone help me get libcurl working?

回答1:

There is no simple answer :) Libcurl depends on other third party libs (it depends on binary distribution that you are using). As you get rid of DLL - you'll have to link with corresponding third parties manually.

Ok, so the first point is that you should not link to libcurl.lib as it binds you to DLL which you don't want to.

Second point - when you are linking with libcurl_static.lib then (as mentioned above) you'll have also to link with libraries it depends on. Simple way to do that is to do something like this:

#if defined CURL_STATICLIB

#if defined _DEBUG
#pragma comment(lib, "libcurl-7.19.3-win32-ssl-msvc\\lib\\Debug\\curllib_static.lib")
#else
#pragma comment(lib, "libcurl-7.19.3-win32-ssl-msvc\\lib\\Release\\curllib_static.lib")
#endif

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libcurl-7.19.3-win32-ssl-msvc\\libeay32.lib")
#pragma comment(lib, "libcurl-7.19.3-win32-ssl-msvc\\openldap.lib")
#pragma comment(lib, "libcurl-7.19.3-win32-ssl-msvc\\ssleay32.lib")

#endif

But this way - you'll get three more dependencies. Alternatively, you can search for a way to link with them statically, but it is a different story.

As another alternative - you could rebuild libcurl_static.lib from sources after disabling all the features you don't need thus removing unwanted dependencies (as described in "Disabling Specific Protocols in Win32 builds" of INSTALL file).

And final point - as libcurl has quite poor support for windows compilation from sources, I'd recommend you to revisit the idea of getting rid of curllib.dll.



回答2:

I got a static build of libcurl to compile and link by specifying both HTTP_ONLY and CURL_STATICLIB in the preprocessor directives of the libcurl project and my application. This eliminates all the dependencies required by protocols you likely do not need. The application now works without requiring any DLLs at all.

Beside the above, I just needed to make sure libcurl.lib and the path to the curl include files were set in the application's visual studio project settings.

References I used:

Disabling Specific Protocols in Win32 builds: http://curl.haxx.se/mail/lib-2011-12/0123.html

Using libcurl in Visual Studio (out-dated): http://curl.haxx.se/libcurl/c/visual_studio.pdf