[英]MinGW cannot link libraries specified when cross-compiling from Linux to Windows
I am trying to compile some code with MinGW from Linux to Windows.我正在尝试使用 MinGW 从 Linux 编译一些代码到 Windows。
My code has two library includes: #include <curl/curl.h>
and #include <fmt/core.h>
.我的代码包含两个库:
#include <curl/curl.h>
和#include <fmt/core.h>
。
When compiling with native g++
, the program compiles without warnings or errors:使用本机
g++
编译时,程序编译时不会出现警告或错误:
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:但是,当我尝试对 MinGW 执行相同操作时,出现以下错误:
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.当我使用
-I
标志时,由于库中的错误,代码不会编译并中止。
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.指定
-L
然后指定目录会产生关于未定义引用的相同输出。
I got the library code from MinGW, from here and here respectively.我分别从这里和这里获得了 MinGW 的库代码。 I put the contents of the
include
directory from the tar.zst
s 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.我将
tar.zst
中的include
目录的内容放入/usr/x86_64-w64-mingw32/include/
并使用/usr/include
中存在的我的 Linux 库验证内容是正确的,尽管有点不同,因为操作系统差异。 I also did the same for the lib
and bin
contents of the libraries.我也对库的
lib
和bin
内容做了同样的事情。
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.
谢谢。
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.要在针对不同平台进行交叉构建时使用库(在您的情况下为 curl 和 fmt),您必须拥有该平台的这些库及其依赖项。 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.您可能还需要使用
-I
编译器标志和-L
链接器标志来告诉您的交叉编译器在哪里可以找到目标平台的这些库的头文件和库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.