Setting Up a Linux -> Windows Cross Compilation Environment

Sometimes you are developing some cross platform application, and maybe using Linux as your development platform, you may not have Windows installed or just lazy to switch. This should be a solution for you .  In this tutorial we are going to setup a cross compilation environment on Linux to compile applications for Windows. At the end of the tutorial you'll have something like this

Setting Up MXE ( MinGW )

First lets setup our cross compiler. We can simply do that by installing mingw-w64, but then we'll have to compile other libraries ourself .. thank god theres a script to do that for us ( Well a Makefile .. ) called MXE.  MXE simply compiles mingw with a lot of other libraries (zlib, boost, sdl, .. etc ), doing that manually would be a tough job.  first go ahead install git if you dont already have it, replace apt-get with whatever tool your dist uses ex : yum .

sudo apt-get install git

then clone the MXE repository .

git clone https://github.com/mxe/mxe

MXE by default only sets up a x32 compiler, if you also want a x64 one go to your cloned directory and open settings.mk with your favorite editor and uncomment this line :

x86_64-w64-mingw32.static x86_64-w64-mingw32.shared

now open a terminal and change to your cloned directory :

cd /path/to/mxe

and then run :

make

this will take a long time to finish, even up to hours because it downloads and compiles a lot of libraries depending and your CPU and Internet connection. If you don't need all of the libraries for now just run :

make gcc qtbase

and when ever you need something else just make again  ex : make boost gtk, Heres a full list of what libraries can MXE compile.

Now you actually have a cross compiler set up, but we also want an IDE .  Before setting up an IDE lets add the new compiler to the environment path. open /etc/environemnt with an editor with root privilege .

sudo gedit /etc/environment

Add the path of the compiler executables, if you cloned the repo in your home directory it should be :

/home/someusername/mxe/usr/bin/

So now your environment file should look something like this :

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/username/mxe/usr/bin"

This wont take effect until you restart your computer or at least run source /etc/environment for the current terminal session .  now executing i686-w64-mingw32.static-g++ should work.  You can continue without it anyways, unless your going to compile from the terminal, in that case you'll have to specify the full path of the compiler.

Setting Up QtCreator

QTCreator is an Awesome C++ IDE. I use it for all of my development (and no it doesn't need to be a qt application).

you can installing with :

sudo apt-get install qtcreator

Go ahead and open it. First thing I do when installing an IDE is change it to the Dark Color, because it looks much better you can skip this if you want.

Tools  > Options > Text Editor > Color Scheme = Inkpot

For our IDE to work we need to set up qmake, so go back to your mxe directory

/path/to/mxe/usr/bin

and find a file called i686-w64-mingw32.static-qmake rename it to qmake or else qtcreator wont find it

Now go to Tools > Options > Build & Run > Qt Versions > Add  Select the file we just renamed and then give the version a name something like QT_WIN_32 .

Next swicth to Compilers tab and press Add > MinGW  name your new compiler MinGW_32 or similar then choose the path it should be like this if you have mxe in your home .  /home/username/mxe/usr/bin/i686-w64-mingw32.statix-g++

if the ABI is not set like this change it to custom and set it up by your self x86-windows-mysys-pe-32bit or x86-64/64 for x64.

Last thing go to the Kits tab and press Add. call your kit Win32 change the compiler to your new compiler, Debugger to none and QtVersion to the qt version we just set up, and then press OK .. its OK if there a waring triangle .

Actually now your all setup to compile your applications for windows. you can test this by Creating a New Project  File > New File Or Project > Non-Qt-Project > Plain C or C++ Application . Name it App and press Next, here you have to select the Win32 Kit We setup earlier , Next Then Finish ..  Add this simple example code :

#include <windows.h>  int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)   {     MessageBoxA(NULL, "Compiled On Linux :D", "Cross Compiled", MB_OK);     return(0); }

Press the hammer tool to build and here you go you have a compiled exe :D .. you cant really test on Linux unless you have wine installed , Wine should work well with normal programs but for advanced project it may be buggy or may not work at all ..

Setting up Wine

This is actually pretty simple just run  sudo apt-get install wine

now navigate to your exe directory and double click it :D Yaaaay !!, It Works :D .

Thats it your all set up .

Notes :

  • xxx-minge-xx.static is for static linking and the .shared if for shared, u can have one compiler for both by using removing .static or .shared fomr the names in the setting.mk but for some what reason it'll say you using the deprecated version but i haven't got any problems with it though.
  • if you want a 64 bit Kit and you cant rename both qmake's to qmake since they are in the same directory just move one of them to a new folder

If you have any question just leave a comment or send me an email :D .