问题
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
$ apt install mingw-w64
and downloaded 500 MB of packages, but I cannot find out how to actually run it. There is no mingw
executable, so how do I actually compile with it?
回答1:
If you look at the file lists on the Ubuntu package webserver for mingw-w64
's constituent packages:
- gcc-mingw-w64-x86-64
- g++-mingw-w64-x86-64
- binutils-mingw-w64-x86-64
- mingw-w64-x86-64-dev
- gcc-mingw-w64-i686
- g++-mingw-w64-i686
- binutils-mingw-w64-i686
- mingw-w64-i686-dev
You can see that mingw-w64
provides a toolchain, i.e. a set of alternative tools (compiler, linker, headers, etc.) used to compile your code for another system.
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 CXX
environment variable to tell most Makefiles to use that compiler to compile code.
回答2:
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.
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.
回答3:
I used this to cross compile 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:
./configure --target-os=mingw32 --cross-prefix=i686-w64-mingw32-
or some
./configure CC=i686-w64-mingw32-gcc ...
etc. GL!
回答4:
Here is how I target mingw g++
cross compiler:
$ ./configure --with-mingw-cross-compiler=g++-mingw-w64-i686
来源:https://stackoverflow.com/questions/15986715/how-do-i-invoke-the-mingw-cross-compiler-on-linux