MinGW cross compiling on linux

2019-03-17 22:18发布

问题:

I have a project which I want to compile for Windows. I have the appropriate makefile, everything works with g++. I ran apt-get install mingw32, downloaded a 100-ish MB package, but I cannot find out how to actually run it. There is no mingw32 executable nor can I find any help on the internet, so how do I actually compile with it?

回答1:

If you see the ouput of dpkg -L mingw32 you can see that the package contains a toolchain i.e. a set of alternative tools (compiler, linker etc) useful to compile your code in another system.

You have to use /usr/bin/i586-mingw32msvc-g++ in order to compile your code. If I remember correctly exists a variable to tell the Makefile to use a toolchain like that.



回答2:

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!



回答3:

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.