Techminded

Standalone C++ app on Windows with Qt framework

Intro

While *nix world gives us power of shared libraries and packaging with dependency management on Windows operating systems you have to either place everything in the same box with your application or hope that user is experience enough to download and install all necessary libraries or virtual machine (ha-ha). The alternative way is to build stadalone or static application that contain all necessary code in it. The good example of quite comprehensive framework is Qt framework that comes from opensource world (and almost free :) and now owned by digia and distributed under both opensource (including free to do everything LGPL) and commrecial licenses. This framework brings you most comprehensive development technologies for almost everything can be needed form desktop application development (it also have official linux and non official other platform mobile versions) and provides nice architecture and syntax sugar that makes development on C++ very close to modern frameworks for  dynamically typed and virtual machine languages.

Environment

So first of all we will download sdk that contains:
Qt Framework libraries and sources
Qt Creator IDE
MinGW compiler (windows port of widely used in Linux GCC compiler)
bundled togeather, the easies way is to download online installer from here:
http://qt.nokia.com/downloads
run it and install framework to default c:\qtsdk, but beaware it will install you much of not really
needed stuff to you can check expert mode and unselect things you don't know except I enumerated earlier. After sdk is installed you can create some examples accessible from Qt Creator IDE compile them and see everything is ok.

Static Qt build


Now we need alternative Qt Framework sources buld statically in order to be able to create statically linked apps.
On the same 'Downloads' page find something like "Qt libraries .... for Windows (minGW ...)", follow the link, decline automatic dowload and choose source code link to .zip file with sources to download.
Unpack archive to C:\QtSDK\Desktop\Qt
Next instructions can differ from one Qt version to another, version that was chosen for the moment of writing this is 4.8.1 as 5 beta won't buid successfully built but as an example I can say it requires also python and even perl interpreters to be installed:)
But I hope you will always can find actual for your version of Qt build instructions by the following link:
http://www.qtcentre.org/wiki/index.php?title=Building_static_Qt_on_Windows
As of 4.8.2 version all we need is to set the following:

QMAKE_LFLAGS = -static -static-libgcc -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc

in file qmake.conf that is located in \mkspecs\win32-g++ folder relatively to unpacked sources folder

The we should configure Qt but run cmd.exe of development environment (you can find the link named 'Qt 4.8.1 for Desktop (MinGW)' in Desktop secion of Qt SDK's Star menu group, run it and go to sources:

 cd C:\QtSDK\Desktop\Qt\qt-everywhere-opensource-src-4.8.2\

and run configure here as follows:

configure -static -release

if you're not get successed you can try to have some magic tuning with options, if ok run

mingw32-make sub-src

that will run nice ha}{or like compilation process that can hypnose you for few minutes of even hours.

When process finished you can open Qt Creator again, go to Options menu item -> Build & Run -> Qt Versions Tab and Add your newly build Qt version here.


Build your project static linked


Then create new project for new environment or configure that already exists to be build with it and add the following file to project .pro file:
CONFIG += static

Now you will have more fat but independent from Qt dlls binary mine GUI hello world weights about 8 mbs if you chosed release mode and now comes the real trick ..

The final stroke

dowload UPX binary from here:
http://upx.sourceforge.net/#downloadupx
and unpack somere not far like to c:\tools
run upx.exe with path to your project binary as parameter from cmd.exe and your will get ~ 3 mbs still fully functional binary. Beaware it will grows while your will try to include some more libraries from framework, but this doesn't look so bad nowdays in comparison to huge and hungry virtual machines. And you actually will not loose any effectiveness when your plaform is not friendly to shared libraries like in our case:)
Have a good development and seamless distribution ;)

Comments