Sunday, July 4, 2010

Static Building for Qt Apps on Windows



I have talked before about dynamic building (click me) and now is the time for the static building. This is a long process that has many steps but I have to share my knowledge for those who are interested because I didn’t find a page that goes throw the entire process in details.
The main reason for static building is collecting all the needed DLLs that you need in a single executable file (*.exe), so the target computer does not need to install any packages or programs.

The steps are as follows:
1-    Configuring Qt Creator for static building, and compiling it.
2-    Building your application project statically using the configured Qt Creator.

1-    Configuring Qt Creator for static building:
Unfortunately, the normal Qt Creator that you install is only configured for static building. So you have to do one of the following:

1.1-    Download the Qt-Everywhere project and build your own customized Qt Creator:
-    This is done by downloading the (qt-everywhere-opensource-src-4.6.3.zip)
-    Uncompress the file in a directory like C:\Qt\4.6.3
-    Use Visual Studio Command Prompt to browse to this folder (is should be containing a ‘configure’ file).
-    Assuming you have Visual Studio 2005 or 2008, configure this project for windows with a command like this from Visual Studio Command Prompt (not tested):

configure -platform win32-msvc2008 –static –release -nomake demos -nomake examples –opensource -confirm-license -no-exceptions

for more configure options: type
configure –help
or download this file.

This may take about 30 minutes, and when done you can type
nmake sub-src
    then just relax and find something to do in the next two hours (give or take an hour)

1.2-    If you have already installed Qt Creator on your PC:
You can reconfigure it to build statically by writing the following command in the ‘Qt Command Prompt’ (I prefer taking a copy and configuring it, so I use the original for developing and debuggingm, and the new one for the final build):
configure -static -release -no-exceptions -nomake demos -nomake examples –opensource -confirm-license

then when done after about 30 minutes or less, type
mingw32-make sub-src

then just relax and find something to do in the next two hours (give or take an hour)


Now you have a Qt Creator configured for static building.


2-    Building your application project statically using the configured Qt Creator:

2.1 Create any Qt project you want, than add the following lines in the project (*.pro) file:
win32 {
      QMAKE_LFLAGS += -static-libgcc
      }

2.2 Now go to the Qt Command Prompt and browse to the folder of your application and type
C:\Qt\Qt-2010.01-static\qt\bin\qmake helloworld.pro

Where:

helloworld.pro is the project file of my Qt application.

qmake is the file used to create the Makefile.

C:\Qt\Qt-2010.01-static\qt\bin\ is the directory of qmake in the newly compiled Qt Creator.


2.3 Now write
mingw32-make

2.4 The statically built application can be found in the ‘Release’ folder in your project.

Some Comments:

1-    Why bothering myself and doing all these steps? For me, nothing is wrong with adding the needed DLLs in the same folder with the dynamically built application.
2-    There is this lovely warning that appears while configuring Qt:
WARNING: Using static linking will disable the use of plugins. Make sure you compile ALL needed modules into the library.
So I have to make sure that all the plugins that I may use in the future are available. (Click here for more:step3)


Example: Simple Map


Sources:

No comments:

Post a Comment