交叉編譯的C Windows libcurl無法在Ubuntu上正確鏈接

[英]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:

我目前正在嘗試在Ubuntu 64x 14.04上為Windows 32x交叉編譯libcurl。經過一些研究后我遵循了以下步驟:

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

1)從https://curl.haxx.se/download.html下載庫

2) Go into extracted libcurl folder and execute:

2)進入解壓縮的libcurl文件夾並執行:

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

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

3) Execute: make

3)執行:make

4) Execute: sudo make install

4)執行: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.

我偶然發現的一件非常有趣的事情是,如果你調用curl-config,你會得到一堆編譯器選項。

3 个解决方案

#1


2  

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

交叉編譯庫使用--prefix定義頂級安裝目錄。

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

Libs將被放入/ usr / i686-w64-mingw32 / lib

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

包含文件的內容與/ 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

使用-L / usr / i686-w64-mingw32 /你指向庫的錯誤路徑並且交叉編譯器找不到libcurl

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

要指向正確的包含位置,您必須在命令中添加-I / usr / i686-w64-mingw32 / include。

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

最后你只編譯了curl libs static,然后你想靜態編譯它們:在你的命令中添加-static。

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 

#2


1  

From the curls FAQ:

從卷發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:

如果您收到鏈接器錯誤,例如“unknown symbol __imp__curl_easy_init ...”,則表明您已鏈接到錯誤的(靜態)庫。如果要使用libcurl.dll並導入lib,則不需要任何額外的CFLAGS,但使用下面的導入庫之一。這些是由各種lib / Makefile。*文件生成的庫:

   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

嘗試使用鏈接器-lcurl_imp或-llibcurl_imp的路徑

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

更新:這是我的Ubuntu與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.

為什么我使用libcurl_imp.lib而不是libcurldll.a,如上表所示?我用cmake構建了curl,它創建了libcurl_imp.lib。所以你應該檢查構建庫的名稱。

#3


0  

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.

這些是交叉編譯編譯器mingw32的一些提示和技巧,以及使用我缺少的參數-DCURL_STATICLIB編譯curl。我沒有測試這個,因為我沒有卷曲解決了問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/05/20/e20997424618cbac714bde0eb17c710e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com