By default the Qt5 libraries distributed from qt.io are dynamically linked. This means that every Qt app dynamically references and uses the Qt prebuild libraries (.dll or .so) it depends on. So in order to deploy your Qt app to your users, you need to find the relevant Qt libraries and distribute them as well (or make sure all other PCs have exactly the same Qt environment as your development PC), which is a pain. That is why it’s easier to build a static version of your application – one single standalone executable with all libraries included inside. But to build a standalone executable of an app you need a static version of Qt libraries. If you target Windows 7/8, you might want to use the Powershell described in qt.io documentation to build a static Qt for Windows, but you can also compile a static Qt5 from scratch on your own. And no, it’s not that difficult…
First you need to make sure you have installed the Qt5 source files. In my case I had already installed Qt5 libraries in my Windows 7 development virtualbox using the Qt unified online Installer but at that time I had not selected to install the sources as well. But that’s not a problem anyway. Once you have Qt5 installed, you can run the Qt Maintenance Tool. Select Add or remove components, and in the “Select Components” dialog click to expand the Qt 5.5 item and enable the Source Components as in the screenshot.
Click Next and the source code download will start, which will take some time since it’s over several hundreds of MB.
Once you have the Qt5 sources installed, you should see a new folder “Src” inside C:\Qt\5.5
Now we need to make some changes to qmake.conf so that we can compile a new static version from those sources we just downloaded.
Open File Explorer and go to C:\Qt\5.5\Src\qtbase\mkspecs\win32-g++\
We need to edit qmake.conf. For sanity, make a backup copy of the original qmake.conf (say qmake.conf.orig), then open qmake.conf with Notepad and add the following lines at the end of the file:
QMAKE_LFLAGS += -static -static-libgcc
QMAKE_CFLAGS_RELEASE -= -O2
QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer
DEFINES += QT_STATIC_BUILD
Note: In the QMAKE_LFLAGS line the -static option eliminates the dependency to the runtime library mingwm10.dll (Note: we might also use a configure option -no-exceptions at compile so that exceptions are not allowed since mingwm10.dll will not exist for taking care of memory handling). The -static-libgcc option removes the dependency to libgcc_s_dw2-1.dll.
Now save the file and close it.
We are ready to compile our static Qt5!
Open a Qt environment command prompt as follows: from Start Menu go to All Programs > Qt > 5.5 > MinGW 4.9 (32 bit) and click “Qt 5.5.0 for Desktop (MinGW 4.9 32 bit)”
In the command prompt, just go to the Src folder:
Once there, configure the new static Qt5 build with the command (IMPORTANT NOTE: Be careful when copy/pasting the following line in your terminal/command line, because the double quotes (around “C:\Qt\Qt5_static”) are not well interpreted. They are modified to something like ; and thus you might get errors like “mkdir: cannot create directory `C:/Qt/5.6/Src/qtbase/bin/?C:’: No such file or directory’” during the final install step. To avoid this kind of error and frustration, paste the line and change the bad double quotes by real ones, before pressing Enter. Thanks Cricri042 for the info. ):
configure -static -platform win32-g++ -prefix "C:\Qt\Qt5_static" -debug-and-release -opensource -confirm-license -nomake examples -nomake tests -nomake tools -opengl desktop -no-angle -qt-sql-sqlite -make libs -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype
and finally run the following two commands to make the actual compile (first command, might take some hours depending on your PC) and install (the second one)
mingw32-make -k -j4
mingw32-make -k install
Once the compile and install finishes, you should have a new folder named Qt5_static inside C:\Qt. Congratulations! You have just build your own static Qt5 toolkit.
Big Note: Do not panic if after hours of compiling (the first command above) you see errors such as
mingw32-make: Leaving directory 'C:\Qt\5.5\Src\qtdeclarative\src/qml'
makefile:217: recipe for target 'module-qtdeclarative-make_first' failed
mingw32-make: *** [module-qtdeclarative-make_first] Error 2
Just ignore those errors, and continue with the last command (mingw32-make -k install) to install the static Qt5. The same errors might appear during install – ignore them as well and proceed below. Nevertheless, if you really want to fix them, you can try what this patch says: adding in your Src/qtbase/mkspecs/features/default_post.prf file the line
static:win32: QMAKE_LFLAGS += $$QMAKE_LFLAGS_STATIC_LIB
After the static Qt5 is compiled and installed in the desired folder (Qt5_static in my case), there is one final step: You need to patch Qt’s installed mkspecs for static build of applications. To do that, using Windows Explorer go to directory C:\Qt\Qt5_static\mkspecs\win32-g++\ and open qmake.conf in Notepad. Add
CONFIG += static
and save it. This makes sure that when you compile your app against the static Qt5, you end up with a static standalone executable.
You are ready.
To compile your application with the static Qt5 and Qt Creator, you need to follow the easy configuration steps described here. As you will see, all you have to do is to add the new static Qt5 as a Qt Kit to your QtCreator setup. Then, whenever you want to build a static version of your app you just select and compile against that static Kit.
If your standalone app binary gets big, you can try make it smaller with tools such as upx (http://upx.sourceforge.net)