sunnuntai 8. elokuuta 2010

Cross-compiling Qt applications for Windows, the easy way

For a while I've been looking for a way to cross-compile my Qt app for Windows on Linux. I don't have any Windows PC's at home or at office, so native build is out of question. There are several instructions how to do this, but they involve cross-compiling the whole Qt from source and a lot of other manual work.

I found and easier way using Wine.

1. Install Nokia Qt SDK for Windows using Wine. It should install fine.

2. Create a build script that does something like this:

export QTDIR=$HOME/.wine/drive_c/Qt/2010.04
export Path=C:\\windows\\system32\\\;C:\\windows\\\;C:\\windows\\system32\\wbem\;C:\\Qt\\2010.04\\mingw\\bin\\\;C:\\Qt\\2010.04\\qt\\bin\\\;\
C:\\Program\ Files\\NSIS\\
cd app_directory
wineconsole --backend=curses qmake.exe
wineconsole --backend=curses mingw32-make.exe

3. That's it! You should now have a working .exe in yout project directory. You can run it using wine.

When shipping your application, you need to include the required mingw and Qt DLL's with it. For example:

$QTDIR/qt/bin/mingwm10.dll
$QTDIR/qt/bin/libgcc_s_dw2-1.dll
$QTDIR/qt/bin/QtCore4.dll
$QTDIR/qt/bin/QtGui4.dll
.. and any other DLL's your app needs. Remember to use the d-versions if you build your app in debug mode.

For SQL drivers install them into directory named sqldrivers in the directory where your .exe is installed. For SQLITE the DLL is $QTDIR/qt/plugins/sqldrivers/qsqlited4.dll.

If you want to create an installer, I can recommend the open source Nullsoft installer (NSIS).