How do I invoke the MinGW cross-compiler on Linux?
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.
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.
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!
Here is how I target mingw g++
cross compiler:
$ ./configure --with-mingw-cross-compiler=g++-mingw-w64-i686