Mingw无法链接从Linux交叉编译到Windows时指定的库

发布于 2025-02-09 13:18:12 字数 3880 浏览 1 评论 0原文

我正在尝试通过从Linux到Windows的MINGW编译一些代码。

我的代码有两个库,其中包括:#include< curl/curl.h>#include< fmt/core.h>

使用本机g ++编译时,该程序无警告或错误编译:

g ++ *.cpp -std = C ++ 2A -O2 -DNDEBUG -WERROR -WERROR -WERROR -WERROR -LCURL -LFMT -LFMT -LFMT -O MAIN

但是,当我尝试使用mingW做同样的事情时,我会收到以下错误:

X86_64 -W64 -W64 -MINGW32 -G ++ *.cpp -std = C ++ 2A -O2 -DNDEBUG -WERROR -WERROR -WERROR -LCURL -LFMT -LFMT -LFMT -o main

/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lcurl: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lfmt: No such file or directory
collect2: error: ld returned 1 exit status

当我直接通过以下内容指定所讨论的文件时,链接器会抱怨未定义的引用。

X86_64-W64-MINGW32-G ++ *.CPP -STD = C ++ 2A -O2 -DNDEBUG -WERROR /USR /USR/usr/x86_64-w64-w64-mingw32/include/curude/curl/curl/curl/curl.h/curl.h/curl.h/curl.h/curl.h/curl.h/curl.h/usr/x86_64-w64-w64-mingw332 /Include/fmt/core.h -o main

/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x8bf): undefined reference to `__imp_curl_easy_init'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x8ce): undefined reference to `__imp_curl_easy_setopt'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x910): undefined reference to `__imp_curl_easy_perform'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x919): undefined reference to `__imp_curl_easy_cleanup'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccXJhCsI.o:console.cpp:(.text+0x55d): undefined reference to `fmt::v8::vformat[abi:cxx11](fmt::v8::basic_string_view<char>, fmt::v8::basic_format_args<fmt::v8::basic_format_context<fmt::v8::appender, char> >)'
collect2: error: ld returned 1 exit status

当我使用-i标志时,由于库中的错误,代码不会编译和中止。

X86_64-W64-MINGW32-G ++ *.CPP -STD = C ++ 2A -O2 -DNDEBUG -WERROR -WERROR -I/usr/usr/X86_64-W64-MINGW32/include/include/curl/curl/curl/-i/usr/-i/x86_64-W64-w64-w64-w64-w64-w64-w64-w64-w64-w64--w64-w64---- mingw32/includ/fmt/-o main

指定-l <​​/code>,然后该目录会产生有关未定义引用的相同输出。

我从mingw获得了库代码,从在这里 =“ https://packages.msys2.org/package/mingw-w64-x86_64-fmt” rel =“ nofollow noreferrer”>在这里。我将的内容includetar.zst s中的目录放入/usr/usr/x86_64-w64-w64-mingw32/include/code/中,并通过我的Linux库中存在/usr/Include中,内容是正确的,尽管由于OS差异而有所不同。我也为libbin库中的内容做了同样的事情。

我在实际项目中使用所有使用的所有使用功能的最小可再现示例将是:

#include <fmt/core.h>
#include <curl/curl.h>
#include <iostream>
#include <string>

size_t _writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
    data->append((char*)ptr, size * nmemb);
    return size * nmemb;
}

int main() {
    std::cout << fmt::format("{:.1f}", 1.2345) << '\n';
    auto curl{curl_easy_init()};
    curl_easy_setopt(curl, CURLOPT_URL, "example.org");
    std::string response_string{};
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _writeFunction);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    curl = NULL;
    std::cout << response_string;
    return 0;
}

有关如何进行的任何帮助。谢谢。

I am trying to compile some code with MinGW from Linux to Windows.

My code has two library includes: #include <curl/curl.h> and #include <fmt/core.h>.

When compiling with native g++, the program compiles without warnings or errors:

g++ *.cpp -std=c++2a -O2 -DNDEBUG -Werror -lcurl -lfmt -o main

However, when I try to do the same with MinGW, I get the following error:

x86_64-w64-mingw32-g++ *.cpp -std=c++2a -O2 -DNDEBUG -Werror -lcurl -lfmt -o main

/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lcurl: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lfmt: No such file or directory
collect2: error: ld returned 1 exit status

When I specify the files in question directly by the following, the linker complains about undefined references.

x86_64-w64-mingw32-g++ *.cpp -std=c++2a -O2 -DNDEBUG -Werror /usr/x86_64-w64-mingw32/include/curl/curl.h /usr/x86_64-w64-mingw32/include/fmt/core.h -o main

/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x8bf): undefined reference to `__imp_curl_easy_init'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x8ce): undefined reference to `__imp_curl_easy_setopt'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x910): undefined reference to `__imp_curl_easy_perform'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccoZVgs5.o:api.cpp:(.text+0x919): undefined reference to `__imp_curl_easy_cleanup'
/usr/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccXJhCsI.o:console.cpp:(.text+0x55d): undefined reference to `fmt::v8::vformat[abi:cxx11](fmt::v8::basic_string_view<char>, fmt::v8::basic_format_args<fmt::v8::basic_format_context<fmt::v8::appender, char> >)'
collect2: error: ld returned 1 exit status

When I use the -I flag, the code does not compile and aborts because of errors within the libraries.

x86_64-w64-mingw32-g++ *.cpp -std=c++2a -O2 -DNDEBUG -Werror -I /usr/x86_64-w64-mingw32/include/curl/ -I /usr/x86_64-w64-mingw32/include/fmt/ -o main

Specifying -L and then the directory produces the same output about undefined references.

I got the library code from MinGW, from here and here respectively. I put the contents of the include directory from the tar.zsts into /usr/x86_64-w64-mingw32/include/ and verified with my Linux libraries present inside /usr/include that the content is correct, albeit a bit different because of OS differences. I also did the same for the lib and bin contents of the libraries.

A minimally reproducible example where I use all of the used functionality in the actual project would be:

#include <fmt/core.h>
#include <curl/curl.h>
#include <iostream>
#include <string>

size_t _writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
    data->append((char*)ptr, size * nmemb);
    return size * nmemb;
}

int main() {
    std::cout << fmt::format("{:.1f}", 1.2345) << '\n';
    auto curl{curl_easy_init()};
    curl_easy_setopt(curl, CURLOPT_URL, "example.org");
    std::string response_string{};
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _writeFunction);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    curl = NULL;
    std::cout << response_string;
    return 0;
}

Any help on how to proceed would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2025-02-16 13:18:12

要使用库(在您的情况下,卷曲和FMT),当使用其他平台进行交叉构建时,您必须具有这些库及其对该平台的依赖关系。
您可能还需要使用-i编译器标志和-l <​​/code>链接标志来告诉您的跨互化器在哪里可以找到目标平台的这些库的标题文件和库。

To use libraries (in your case curl and fmt) when cross-building for a different platform you must have those libraries and their dependencies for that platform.
You may also need to use -I compiler flags and -L linker flags to tell your cross-compiler where to find the header files and libraries of those libraries for the target platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文