36

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?

5
  • 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++" Commented Apr 13, 2013 at 10:20
  • Is the Makefile generated using the autotools? Commented Apr 13, 2013 at 10:26
  • @gipi I honestly can't remember, I use cmake for my projects. Commented Apr 13, 2013 at 10:43
  • @thedaver64 this is important, there are options in cmake to indicate the toolchain Commented Apr 13, 2013 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 :) Commented Apr 13, 2013 at 13:26

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

There is no variable for toolchain, but you can set variables for C and C++ compilers: CC and CXX respectively.
9

Another option is to take a look at Mingw Cross Environment (MXE), which is specifically targeting cross compiling from Linux to Windows (and lately also to Mac). The package has built-in support 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.

2 Comments

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.
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.
8

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!

Comments

2

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.