dimitris kalamaras

mathematics, social network analysis, free software…

Build a static Qt5 for Windows by compiling from sources

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.

qt55-installer-src

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:

cd ..
cd Src

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

qt5-static-compile

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[4]: 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.

Optimization

If your standalone app binary gets big, you can try make it smaller with tools such as upx (http://upx.sourceforge.net)

Read more:
https://blog.qt.io/blog/2012/05/08/qt-commercial-support-weekly-19-how-to-write-your-own-static-library-with-qt-2/
http://wiki.qt.io/Build_Standalone_Qt_Application_for_Windows
http://wiki.qt.io/How_to_build_a_static_Qt_version_for_Windows_with_gcc

Previous

SocNetV version 1.9 is here, with LU decomposition and a Qt showcase!

Next

Bovary.gr goes online – A new digital experience for women

17 Comments

  1. Josef Breitschafter

    Hi, i have one question.
    Will this work also with MSVC2013_64 Compiler and QT5.4 Enterprise?

  2. Verci Nemells

    Hello,

    I’m not one to usually leave some reply on a random site (sorry) such as this.

    However, This guide is thoroughly impressive.

    Thank you very much great guide.

    SUCCESS:
    Compiled on:

    Brand- Hewlett Packard
    Model- DV4-1425dx
    CPU- AMD Turion II @ 2.0GHz
    GPU- ATI 4200 HD Mobility
    RAM- 4GB DDR2 800MHz
    HDD- Intel 128GB SSD
    OS- Windows 10 64bit Enterprize

    SOFTWARE INSTALLED:

    Python34
    Perl

  3. djme

    Hello,
    The configure and make steps work for me, however when I try to run the “mingw32-make -k install” step I receive the following error:

    “filename, directory name, or volume label syntax is incorrect”.

    Terminal Output:

    Qt\Qt5.5.0\src>mingw32-make -k install
    qtbase\ && ( if not exist Makefile C:\Qt\Qt5.5.0\src\qtbase\bin\qmake C:\Qt\Qt5.5.0\src\qtbase\qtbase.pro -o Makefile ) && mingw32-make -f Makefile install
    gw32-make[1]: Entering directory ‘C:/Qt/Qt5.5.0/src/qtbase’
    src\ && ( if not exist Makefile C:\Qt\Qt5.5.0\src\qtbase\bin\qmake C:\Qt\Qt5.5.0\src\qtbase\src\src.pro -o Makefile ) && mingw32-make -f Makefile install
    gw32-make[2]: Entering directory ‘C:/Qt/Qt5.5.0/src/qtbase/src’
    tools\bootstrap\ && ( if not exist Makefile C:\Qt\Qt5.5.0\src\qtbase\bin\qmake C:\Qt\Qt5.5.0\src\qtbase\src\tools\bootstrap\bootstrap.pro -o Makefile ) && mingw32-make -f Makefile install
    gw32-make[3]: Entering directory ‘C:/Qt/Qt5.5.0/src/qtbase/src/tools/bootstrap’
    gw32-make -f Makefile.Release install
    gw32-make[4]: Entering directory ‘C:/Qt/Qt5.5.0/src/qtbase/src/tools/bootstrap’
    filename, directory name, or volume label syntax is incorrect.
    efile.Release:11156: recipe for target ‘install_target’ failed
    ……..
    ……
    ….

    ..
    .

    Do you know what the problem may be? Is the install path in the correct format during the configure step?

    Thanks.

  4. jinnon

    Thanks alot dimitris. this article really helped me to understand how to set up static version while many materials explain general things which need more trial&errors following.

    I actually used windows-build-qt-static.ps1 script file to set up. but I just made it after understanding from your guide what i missed previously.

    thanks again.

  5. Grzegorz

    Error like in other tutorials before 🙁 I’m gettin nervous. Look at error that appears.

    http://s9.postimg.org/mrkto71r3/Zrzut_ekranu_2016_02_17_o_10_30_38.png

    I’ve got QT 5.5.1, MinGW latest from MinGW website because without it i don’t have mingw32-make file.
    Python 3.3 and 2.7
    Ruby 2.2
    Perl Active..something 🙂

    Nothing’s help i’m trying on windows 10 64 bit and now on clean install of windows 7 32 bit.

  6. Patrick

    Hi,

    I tried to build it according to the description here. I was able to build it partially but I always getting 4-5 error’s after the compilation.

    Mainly about the widget’s. When I try to compile a Qt-App without using Widgets,everything works fine, but as soon as I want to use the Widgets or QML I receive error inside the Qt Creator.

    The Qt version I tried is the latest one 5.6.0.

    Has anybody have had success with MinGW-32 and the above version?

    Any help would be highly appreciated.

    Thanks in advance.

    Patrick

  7. Patrick

    Hi again,

    I just wanted to say, that I was able to successfully build Qt5.6.0 with the instructions here on this site with another Intel Quadcore Processor (i5 3770k). I did the same thing with my newer processor (i7 6700) without success.

    I have no idea why there’s a processor dependency. Or maybe the other Tools on my pc are somehow interfering with the Qt-MinGW toolchain, somehow?

    I have now copied to Qt_MinGW_Static-Folder to my newer PC and were able to compile there as well my Qt-Apps statically.

    Kind Regards
    Patrick

  8. Dogebag

    Thanks for that guide. After many attempts and many mingw Versions i got it to compile. But i can’t get it to install as probably there are wrong paths?!

    I get a bunch of those no such directory messages. Ran as admin and followed the configure guide.

    make[2]: Entering directory `/c/Qt/5.6/Src/qtbase/src’
    cd corelib/ && ( test -e Makefile.qtzlib || c:/Qt/5.6/Src/qtbase/bin/qmake.exe C
    :/Qt/5.6/Src/qtbase/src/corelib/qtzlib.pro -o Makefile.qtzlib ) && make -f Makef
    ile.qtzlib install
    make[3]: Entering directory `/c/Qt/5.6/Src/qtbase/src/corelib’
    make -f Makefile.qtzlib.Debug install
    make[4]: Entering directory `/c/Qt/5.6/Src/qtbase/src/corelib’
    mkdir: cannot create directory `C:/Qt/5.6/Src/qtbase/bin/?C:’: No such file or d
    irectory
    make[4]: *** [install_targ_headers] Error 1
    make[4]: Leaving directory `/c/Qt/5.6/Src/qtbase/src/corelib’
    make[3]: *** [debug-install] Error 2
    make[3]: Leaving directory `/c/Qt/5.6/Src/qtbase/src/corelib’
    make[2]: *** [sub-zlib-install_subtargets] Error 2
    make[2]: Leaving directory `/c/Qt/5.6/Src/qtbase/src’
    make[1]: *** [sub-src-install_subtargets] Error 2
    make[1]: Leaving directory `/c/Qt/5.6/Src/qtbase’
    make: *** [module-qtbase-install_subtargets] Error 2

  9. figo

    i get error mingw32-make: Target ‘install’ not remade because of errors.

  10. Cricri042

    Hi,
    For those getting the error like :
    mkdir: cannot create directory `C:/Qt/5.6/Src/qtbase/bin/?C:’: No such file or directory’ during install, just be warned that the problem comes from the “copy/paste” command you made when running the configure.
    if you just copied and paste (in the console) :
    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

    It will fail because the double quotes (around “C:\Qt\Qt5_static”) are not well interpreted, and they are modified by the make file to something like ;
    “?C:\Qt\Qt5_static?” ….
    So the install step will be unable to create the directories and so …

    To resolve the problem, just do a distclean, and re-run configure with the line above (bu by changing the bad double quotes by real one)

    Hoping this help.

    –Christian

  11. Thanks for the info, will edit the post to note the issue.

  12. @dogebag, your problem might also be double-quotes misinterpreted while copy/pasting the ./configure line from the webpage.
    Do a distclean and re-run that configure command changing the bad double quotes with real ones.

  13. @Grzegorz, your problem might also be double-quotes misinterpreted while copy/pasting the ./configure line from the webpage.
    Do a distclean and re-run that configure command changing the bad double quotes with real ones. A commenter indicated that might be the problem some of you experience in the the make install phase.

  14. Amir

    Hi,
    Does it work for a 64bit system?
    It looks like it was able to build a static version of QT on my windows 7 64 bit, but when I try to compile my app there are a few errors that stop the compiling processes. The end result is this:

    Error while building/deploying project RHD2000interface (kit: Desktop Qt 5.5.0 MinGW Static 32bit)
    The kit Desktop Qt 5.5.0 MinGW Static 32bit has configuration issues which might be the root cause for this problem.
    When executing step “Make”

    Cheers,
    Amir

  15. bgibby

    Thanks this worked perfectly, was a great help!

  16. Daniel Kolbach

    I have been working for two days on this thing. In the middle of running the firs command i get a pop up that says g++.exe Application error. The application was unable to start correctly. Closing that popup, says ../bin/qmake.exe failed. Any idea what it could be?

  17. Name

    The configure -qt-sql-sqlite flag is an invalid option for Qt 5.9.2 on my Windows 10 VM. I removed that flag because I don’t need SQLite.

Leave a Reply

Creative Commons License
Licensed under a Creative Commons Attribution-ShareAlike 4.0 International License - Powered by Linux