Cross-compiling for windows (from linux) - Part 1
This article explains how to cross-compile a 64-bit windows application
from linux. This means that you don't need to have a windows build-machine,
and you can use all linux based development tools. This is great for
developers who feel more comfortable in linux.
For some time, I knew that this was technically possible, but always
assumed it would be either too difficult, or otherwise buggy and cumbersome.
That I would eventually give up, and porting the compilation to windows,
using a build machine running windows.
After two days of working at this problem, I'm eager to explain and
share my experience with it. I'll try to do so in two parts:
Part 1:
- Install the mingw cross-compilation tools.
- Compile hello_world.exe.
- A SCons based build system, that will be used in part 2.
Part 2:
- Cross-compile SDL. Cross-compile a program that links to,
and initializes SDL.
- Repeat the process for similar libraries:
- SDL_image, together with libjpg and libpng.
- SDL_mixer, with libogg and libvorbis
- SDL_ttf, with libfreetype
- GLEW
1. Getting mingw, the cross-compiler tools
To cross-compile, I'll use the mingw-project, in
partuclar, the 64 bit gcc-like build tools. On ubuntu, I installed these with
If you're relying on c++11 features, you might want to verify that
the mingw-g++ compiler is version 4.7 or newer.
To check out which version you have:
2. Hello_World.exe
In order to cross-compile a hello world program, all you need to do is:
It
works fine.
The
--static makes sure all necessary libraries get
packed into the
exe.
Without it, you'll get
an error message.
3. Simple SCons build system
Since a project usually consists of more than a main.cpp,
you want an exensible build system. Preferably, one with the ability
to easy switch between target platforms, and manage the differences between
them.
I'll be using SCons.
It is based on python, and easy to understand and customize. In fact, I
don't think you need to know anything about SCons or python
to make sense of the scripts (which rarely is the case for make and cmake)
To follow along, grab a
copy of the repo
(and check out the "basic" branch)
The SCons "Makefile", aka SConsctruct
This sets it up so we can use --win64 flag to specify that we
want a windows build. With this enabled, x86_64-w64-mingw32-g++ is
used as the compiler, instead of g++.
Additionally, the SConstruct script asks SCons to
put all files created by the build in ./build/linux/ or ./build/windows/. This
keeps things tidy, and we also don't need to rebuild everything when alternating
building for the two platforms.
Lastly, is does whatherver the SCons build script src/SConscript
(together with the environment we set up) tells it to do. But, before I show that, I want to show
the two python scripts that will be used to differentiate the details
between the target platforms:
Linux (utils/linux_build.py)
Windows (utils/win_build.py)
The SCons build script, src/SConscript
Building with SCons
Using the SCons setup, we can build a
linux executable (./bin/main) with:
To build a windows executable (
./bin/main.exe):
If you want to clean up, add
-c at the end, e.g.:
scons --win64 -c
To be continued ... (part 2)