C++ 如何在 Linux 上调用 MinGW 交叉编译器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15986715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:58:09  来源:igfitidea点击:

How do I invoke the MinGW cross-compiler on Linux?

c++compilationcross-compilingmingw-w64

提问by Martin Melka

I have a project that I want to cross-compile for Windows. I have the appropriate Makefile and everything works with g++. I've run

我有一个要为 Windows 交叉编译的项目。我有适当的 Makefile,一切都适用于g++. 我跑了

$ apt install mingw-w64

and downloaded 500 MB of packages, but I cannot find out how to actually run it. There is no mingwexecutable, so how do I actually compile with it?

并下载了 500 MB 的软件包,但我不知道如何实际运行它。没有mingw可执行文件,那么我如何实际使用它进行编译?

采纳答案by gipi

If you look at the file lists on the Ubuntu package webserver for mingw-w64's constituent packages:

如果您查看 Ubuntu 软件包网络服务器上的文件列表,了解mingw-w64的组成软件包:

You can see that mingw-w64provides a toolchain, i.e. a set of alternative tools (compiler, linker, headers, etc.) used to compile your code for another system.

您可以看到它mingw-w64提供了一个工具链,即一组用于为另一个系统编译代码的替代工具(编译器、链接器、头文件等)。

Assuming you want to compile C++ code for a 64-bit system, you'll need to use /usr/bin/x86_64-w64-mingw32-g++-win32. You can use the CXXenvironment variable to tell most Makefiles to use that compiler to compile code.

假设您要为 64 位系统编译 C++ 代码,则需要使用/usr/bin/x86_64-w64-mingw32-g++-win32. 您可以使用CXX环境变量告诉大多数 Makefile 使用该编译器来编译代码。

回答by Bart

Another option is to take a look at Mingw Cross Environment (MXE), which is specifically targetting at cross compiling from Linux to Windows (and lately also to Mac). The package has bult-in xupport for a large number of libraries and is actively being developed. Just take a look at the website to find out if it suits your needs.

另一种选择是查看 Mingw Cross Environment ( MXE),它专门针对从 Linux 到 Windows(最近也到 Mac)的交叉编译。该软件包具有用于大量库的内置 xupport,并且正在积极开发中。只需查看该网站,看看它是否适合您的需求。

By the way,it is suggested you use the development rather than the release version. This is because release versions are generally outdated very fast, due to package maintainers (of the libraries) changing URLs resulting in the MXE release version becoming broken. The development version is generally more up-to-date.

顺便说一句,建议您使用开发版而不是发行版。这是因为发布版本通常很快就会过时,这是由于(库的)包维护者更改 URL 导致 MXE 发布版本损坏。开发版本通常是最新的。

回答by rogerdpack

I used this to cross compile postgres:

我用它来交叉编译postgres:

$ sudo apt-get install mingw-w64
$ ./configure --host=i686-w64-mingw32 --without-zlib #  32 bit
# or --host=x86_64-w64-mingw32  64 bit

ref here

参考这里

Other projects do it differently, like ffmpeg:

其他项目的做法不同,比如 ffmpeg:

 ./configure --target-os=mingw32 --cross-prefix=i686-w64-mingw32-

or some

或一些

  ./configure CC=i686-w64-mingw32-gcc ...

etc. GL!

等等。GL!

回答by user8128167

Here is how I target mingw g++cross compiler:

这是我定位mingw g++交叉编译器的方式:

$ ./configure --with-mingw-cross-compiler=g++-mingw-w64-i686