交叉编译的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