Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross compiled C Windows libcurl doesn't get linked correctly on Ubuntu

I am currently trying to cross compile libcurl in c for Windows 32x on Ubuntu 64x 14.04. After a bit of research I followed these steps:

1) Download library from https://curl.haxx.se/download.html

2) Go into extracted libcurl folder and execute:

./configure --host=i686-w64-mingw32 --build=i686-pc-linux-gnu --prefix=/usr/i686-w64-mingw32/ --enable-static --disable-shared

3) Execute: make

4) Execute: sudo make install

Then I added these include statements:

#include <winsock2.h> // Needed for curl
#include <windows.h> // Windows API
#include <curl/curl.h>

int main(int argc, char** argv) 
{
    CURL *curl;
    CURLcode response;

    char url[] = "someurl.com";

    curl = curl_easy_init();
    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, url); //set url options

        /* Perform the request, res will get the return code */ 
        response = curl_easy_perform(curl);

        if(response != CURLE_OK)
        {
          //Do something
        }

        /* always cleanup */ 
        curl_easy_cleanup(curl);
    }

return 0;
}

Now I tried to compile my code with following arguments:

i686-w64-mingw32-gcc main.c -o main.exe -L/usr/i686-w64-mingw32/lib -lcurl

The compiler returned following error code:

/tmp/ccebLf6U.o:main.c:(.text+0x336): Not defined reference to `_imp__curl_easy_init'
/tmp/ccebLf6U.o:main.c:(.text+0x365): Not defined reference to `_imp__curl_easy_setopt'
/tmp/ccebLf6U.o:main.c:(.text+0x372): Not defined reference to `_imp__curl_easy_perform'
/tmp/ccebLf6U.o:main.c:(.text+0x3f4): Not defined reference to `_imp__curl_easy_cleanup'
collect2: error: ld returned 1 exit status

Has someone an idea on how to fix this ?

[EDIT]

Something really interesting I stumbled upon is that if you call curl-config you get a bunch of compiler options.

like image 557
Qubasa Avatar asked May 20 '16 14:05

Qubasa


3 Answers

Cross-Compiling library Using --prefix you are defining the the toplevel installation directory.

Libs will be placed into /usr/i686-w64-mingw32/lib

Same thing for includes files they will be placed /usr/i686-w64-mingw32/include

Using -L/usr/i686-w64-mingw32/ you are pointing the wrong path for libraries and cross-compiler cannot find libcurl

To point to the correct include location you have to add -I/usr/i686-w64-mingw32/include to your command.

At the end you compiled curl libs static only then you want to compile them statically: add -static to your command.

SO the correct command will be:

i686-w64-mingw32-gcc -static -I/usr/i686-w64-mingw32/include -L/usr/i686-w64-mingw32/lib -lcurl main.c -o main.exe 
like image 121
LPs Avatar answered Sep 29 '22 13:09

LPs


From the curls FAQ:

If you get linker error like "unknown symbol __imp__curl_easy_init ..." you have linked against the wrong (static) library. If you want to use the libcurl.dll and import lib, you don't need any extra CFLAGS, but use one of the import libraries below. These are the libraries produced by the various lib/Makefile.* files:

   Target:          static lib.   import lib for libcurl*.dll.
   -----------------------------------------------------------
   MingW:           libcurl.a     libcurldll.a
   MSVC (release):  libcurl.lib   libcurl_imp.lib
   MSVC (debug):    libcurld.lib  libcurld_imp.lib
   Borland:         libcurl.lib   libcurl_imp.lib

Try path to linker -lcurl_imp or -llibcurl_imp

Update: Here is write flags on my Ubuntu with MinGW64:

i686-w64-mingw32-g++ -o app.exe objects.a -Lexternals/curl-7.39.0/lib -llibcurl_imp

Why I use libcurl_imp.lib instead libcurldll.a as described in table above? Becouse I build curl with cmake which make libcurl_imp.lib. So you should check name of built library.

like image 38
SergA Avatar answered Sep 29 '22 13:09

SergA


So my solution to this problem probably lies right here: Cross compile tips for libraries

These are some tips and tricks for the cross compilation compiler mingw32 and the compilation of curl with my missing argument -DCURL_STATICLIB. I didn't test this out though because I solved the problem without curl.

like image 44
Qubasa Avatar answered Sep 29 '22 13:09

Qubasa