Building a Cross compiler for Windows on Linux
This is a small How-To on creating a cross compiler for Windows that runs on Linux.
Intro
Why would one ever do something as stupid as compiling Qt/Windows applications on Linux? There are several (good and better) reasons to do this:
- You don't feel comfortable working under Windows.
- The shell on Linux is far more powerful than Windows own cmd.exe.
- You don't want to reboot your workstation just to do another compile.
- You don't want to transport your working copy of the source tree on a floppy or memory stick, just because Windows is too dumb to read most Linux file systems.
- You don't want to leave your known and comfortable programming environment.
- You don't want to search, download and install your favourite scripting language to do something useful, just because Windows did not deliver it in the base package.
- Your Windows is so crippled and restricted that you can't even install simple things (many admins in big companies like this sport).
- In short: you don't want to waste good development time.
This article will show you how to create an environment on Linux, so that you don't need to leave it for compiling Windows applications. Unfortunately you'll still need Windows to test these applications.
Getting the sources
If you installed Qt/Win32 with MinGW before, just copy the MinGW source files (c:\MinGW\src\*.gz) from Windows to Linux.
Otherwise download the source packages from: http://mingw.sourceforge.net/download.shtml, you'll need these packages (the versions in parenteses are the current ones I tested with, newer ones should work as well):
- binutils (2.15.94)
- gcc-core (3.4.2)
- gcc-g++ (3.4.2, must match gcc-core!)
- mingw-runtime (3.7)
- w32api (3.2)
Creating the MinGW cross-compiler
It is assumed that:
TARGET=i586-mingw32
PREFIX=$HOME/mingw
- Unpack all archives
- compile binutils:
./configure --target=$TARGET --prefix=$PREFIX
make && make install
- Copy the header files to the target:
mkdir -vp $PREFIX/include
cp -r mingw-runtime-*/include/* $PREFIX/include
cp -r w32api-*/include/* $PREFIX/include
- Compile GCC for C only (you need C for the libs and the libs for C++):
./configure --prefix=$PREFIX --target=$TARGET --enable-threads --enable-languages=c
make && make install
- Compile the Windows API:
./configure --prefix=$PREFIX --target=$TARGET --host=$TARGET --build=$(./config.guess)
make && make install
- Compile the Windows Runtime:
mv w32api-* w32api
cd mingw-runtime*
./configure --prefix=$PREFIX --target=$TARGET --host=$TARGET --build=$(./config.guess)
make && make install
- Re-compile GCC for C & C++:
./configure --prefix=$PREFIX --target=$TARGET --enable-threads --enable-languages=c,c++
make && make install
It might happen that GCC has problems with linking, the solution is easy:
The install scripts create $PREFIX/lib and $PREFIX/$TARGET/lib - move everything from
$PREFIX/$TARGET/lib to $PREFIX/lib and then make the former a link to the latter:
cd $PREFIX/$TARGET
rmdir lib
ln -s ../lib lib
How to proceed
From there on you can use the MinGW C/C++ compiler just like any other version of GCC.
In Part 2 of this article I will explain how to cross compile Qt4/Win32.
In Part 3 I will explain how to compile Qt applications using this cross compiler.