Touching Windows with a barge-pole
Saturday, November 24th, 2007We are a Linux shop here at MakeSweet, but occasionally we need to distribute programs - and that means compiling for Windows. We’ve found that for the cleanest results, MinGW (Minimalist GNU for Windows) is hard to beat. It is useful even if you are compiling on Windows, but it is especially useful if you are cross-compiling from Linux.
Getting set up for cross-compiling for Windows is trivial to set up on Debian/Ubuntu, just do:
sudo apt-get install mingw32
For other Linux systems, you can follow some tips on the MinGW wiki: BuildMingwCross, Build-a-Win32-x-compiler-for-Linux.
Our favorite build system is CMake; there are tips on adapting CMake to MinGW on the CMake wiki at CMakeMingw.
One subtlety with MinGW is that after building, your program may depend on mingw10.dll. This is a freely distributable DLL (on Debian, you can find it in /usr/share/doc/mingw-runtime) but it can be nice to build programs with no extra DLL dependencies. To do that, make sure the “-mthreads” flag is not used during compilation, and remove it if it is. You should be aware of the consequences of removing the flag though, and it may not be a reasonable thing to do. In our experience, if you are not using exceptions, things seem ok without it.
For cross-compiling with MinGW on Linux, CMake helps a lot. See CMakeCrossCompiling for tips. At the time of writing, only the CVS version of CMake supports cross-compiling. You just need to set up a “toolchain” file describing your environment, for me I called it ~/mingwin.cmake and it looked like this:
SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_C_COMPILER /usr/bin/i586-mingw32msvc-gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/i586-mingw32msvc-g++)
SET(CMAKE_FIND_ROOT_PATH /usr/i586-mingw32msvc /home/bozo/mingw/install)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Then, when configuring a project, the first call to CMake would be like this:
cmake -DCMAKE_TOOLCHAIN_FILE=~/mingwin.cmake .
