33

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?

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Martin Melka
  • 7,177
  • 16
  • 79
  • 138
  • If you run the following command from a terminal it should tell you the executable of the compiler ( g++ in this example case ). You can then edit your makefile appropriately. "locate mingw | grep g++" – thedaver64 Apr 13 '13 at 10:20
  • Is the `Makefile` generated using the autotools? – gipi Apr 13 '13 at 10:26
  • @gipi I honestly can't remember, I use cmake for my projects. – thedaver64 Apr 13 '13 at 10:43
  • @thedaver64 this is important, there are options in `cmake` to indicate the toolchain – gipi Apr 13 '13 at 11:10
  • @gipi In cmake I just issue a "cmake -G "MinGW Makefiles" for compiling on windows and "cmake -G "Unix Makefiles" when compiling on my linux box to have the Makefile built. Is this what you're after? If not, you'll need to elaborate :) – thedaver64 Apr 13 '13 at 13:26

4 Answers4

21

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

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.

Community
  • 1
  • 1
gipi
  • 2,432
  • 22
  • 25
8

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.

Bart
  • 2,062
  • 15
  • 19
  • There's no mention about Mac on the website? This seems pretty neat, I'm new to this, so I'm guessing your tool creates the "cross-compiler" first, and then I just have to use your created toolchain to compile things. – CMCDragonkai Jun 27 '15 at 10:58
  • Well, it's not "mine", but that's pretty much what it does. Not sure about the current state regarding Mac support (I haven't used mxe in a while). It has been mentioned in the past that they were working on support, but I have no idea if it's actually been implemented. – Bart Jul 01 '15 at 11:29
7

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!

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
2

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

$ ./configure --with-mingw-cross-compiler=g++-mingw-w64-i686
user8128167
  • 6,929
  • 6
  • 66
  • 79