Newsletter

Subscribe to our newsletter to receive all the latest on new products and offers.

Partners
Follow us

Building Static Qt For Visual Studio

Due to many requests, I will show you in this article how to setup Nokias "Qt" in Visual Studio for small static builds. The main advantage of static builds is that you don't have to ship all the dynamic libraries with your product to the user. This makes your application more portable and smaller.

First step is to download the qt sourcecode.
You can find it at: http://qt.nokia.com/downloads/windows-cpp

Unzip the archive to your favored destination.

Now create the environment variable QTDIR and point it to the Qt directory.

Setting QT Path in Windows Environment Variables Dialog

In the next step you have to edit qmake.conf in "..qt\mkspecs\win32-msvcXXXX".
e.g. if you have Visual Studio 2008 you can find the config file in the win32-msvc2008 folder.

In order to create a static release, change the content:

QMAKE_CFLAGS_RELEASE = -O1 -MD
QMAKE_CFLAGS_DEBUG = -Zi -MDd

to:

QMAKE_CFLAGS_RELEASE = -O1 -MT
QMAKE_CFLAGS_DEBUG = -Zi -MTd

Flag -O1 creates smaller and -O2 faster code. MT stands for multi threaded static linking.

In the next step you can customize the build process by using "configure.exe".
Start it with the parameter "-h" to see all available options.

Of course the most important parameter for a static build is "-static".
For smaller builds and less compilation time, remove all unwanted features.(*)

Beware of using the link time code generation option "/ltgc" in combination with a static build. I noticed some side effects like an enormous library or increase of compilation/linking time in Visual Studio.

You are now ready to compile Qt. Open the command shell and go to the Qt directory.
Get the environment setting of visual studio to get access to the nmake tool.
e.g. "C:\Apps\Dev\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"

Call nmake.exe (Microsoft Program Maintenance Utility) and start the build process. Dependent on the required features and your computer performance it will take a few minutes till hours.

The smallest size of a poor Qt-Gui-Application I could achieve, was something about 3.7MB with Qt4.62. Optionally, you can shrink it with a binary packer like UPX to merely 1.7MB.

You can also speed up build time by using a multithreaded version of nmake.
The tool is called JOM and makes use of multi core processors. I could achieve around 30-40% speed improvement by using two cores. Just copy the file to your Qt directory and use JOM instead of nmake.

(*) It is always a good idea to write the whole setup in a batch file for future releases.

Example-File:

@echo off
set t0=%time%
set no_extras=-nomake examples -nomake demos
configure.exe -static -opensource -platform win32-msvc2008 %no_extras%
call "C:\Apps\Dev\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
nmake
echo config started:%t0%
echo config ended:%time%
@echo on

Ways to support us
5 simple ways how you can support our project.
Read more...
Try out TimeComX Pro