I am trying to use visual studio 2015 to statically link a small test curl program, however i cannot get it to link properly.
I used this batch file to compile curl which worked successfully https://github.com/blackrosezy/build-libcurl-windows
I then copied the libcurl directory into my project directory and my code is the following
#include "stdafx.h"
#include "libcurl/include/curl/curl.h"
#pragma comment(lib, "libcurl/lib/static-debug-x64/libcurl_a_debug.lib")
#define CURL_STATICLIB
int main()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if (curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
printf("Press any key to continue\n");
getchar();
return 0;
}
However no matter what i do i cannot get my linker to grow out of doing this:
1>CurlTest.obj : error LNK2019: unresolved external symbol __imp_curl_global_init referenced in function main
1>CurlTest.obj : error LNK2019: unresolved external symbol __imp_curl_global_cleanup referenced in function main
1>CurlTest.obj : error LNK2019: unresolved external symbol __imp_curl_easy_init referenced in function main
1>CurlTest.obj : error LNK2019: unresolved external symbol __imp_curl_easy_setopt referenced in function main
1>CurlTest.obj : error LNK2019: unresolved external symbol __imp_curl_easy_perform referenced in function main
1>CurlTest.obj : error LNK2019: unresolved external symbol __imp_curl_easy_cleanup referenced in function main
1>U:\Main\Code\CurlTest\x64\Debug\CurlTest.exe : fatal error LNK1120: 6 unresolved externals
I have confirmed all of these paths are valid, and have tried using both debug and release libs, and 32 and 64 bit (With matching settings in visual studio). This will compile with a non-static library, but that's crap as i'd like to just distribute my .exe not dll's as well.
What am i doing wrong here? This is very frustrating, it looks like it's just being childish as from what i've read from similar threads that #define CURL_STATICLIB directive should rectify this behaviour.
#pragma comment(lib, "wldap32.lib" )
#pragma comment(lib, "crypt32.lib" )
#pragma comment(lib, "Ws2_32.lib")
#define CURL_STATICLIB
#include <curl/curl.h>
this made it work for me.
hope it helps, regards.
I'll guide you through how to think of this, to complement Gigi's answer.
#define CURL_STATICLIB
is a preprocessor directive. It will affect code present in the same "file", and only appearing after it.
Preprocessor directives do not affect linked-in code. Linking is a separate phase of the compile process. In essence, consider: it cannot affect libcurl.lib
. It can only affect the 'current file'.
Let's consider your code next. Your code happens not check for CURL_STATICLIB
. But let's, for the sake of experimenting, assume that defining CURL_STATICLIB
will change something.
If this preprocessor directive were really to affect something as other people suggest, then it must be that it affects other code that was #include
d via preprocessor into this particular file. That is, it probably affects something in curl/curl.h
.
In short... have you tried moving the #define
either to: (a) project settings, or (b) just above #include <curl/curl.h>
?
Note that <curl/curl.h>
in itself would be the correct phrasing for your #include "libcurl/include/curl/curl.h"
. If you were ever to build your code on other platforms or from shared code available on a platform, you'd want to include your stuff in the 'standard' way. It is nearly always a good practice to plan for portability.
And cURL's headers would usually get installed systemwide (hence <>
) into /usr/include/curl/...
(hence curl/...
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With