How to compile Qt 5 under Windows or Linux, 32 or 64 bit, static or dynamic on Visual Studio or g++

Note: There’s also another article I wrote to compile from GIT source here with an automated script on Windows. You may modify it for Linux as the difference is shown in this post.

This article is continuously being updated. If this helps you, please give it a “thumbs up” so that I could know that it’s helping people and not being useless.

If you have any comments or you found typos, please let me know so that I can fix them.

First thing, it doesn’t matter whether you want to compile 32 or 64 bit version. The only difference is the command prompt shortcut that you have to choose from Visual Studio, which will initialize different environment variables.


Let’s begin with this:

  1. Download and install Perl: Download link

  2. Download and install Python: Download link

  3. Download and install Windows SDK (probably not necessary, but recommended)
    I use Windows 8, so this is the version I used: Download link
    ,Otherwise find the proper version for your Windows.

  4. Download and install DirectX SDK (probably necessary if you wanna compile with OpenGL)
    Download link

  5. Download and extract jom to some folder (not needed for linux) (jom is a tool for compiling stuff with VS in parallel, there’s a way to do this with nmake, but I’m not familiar with it) Download link

  6. Download Qt Opensource, and extract it, say to C:\Qt\Qt5.6, so now the folder qtbase can be found in C:\Qt\Qt5.6\qtbase .

  7. Only for Windows: DO YOU REALLY WANT IT TOTALLY STATIC?

    Usually, even if you choose the compilation to be static, the compiler itself will still not merge its libraries statically. If you want your compiled source to be fully static with respect to the compiler (Visual Studio), you have to do this tweak in the QMAKE files of Qt.

    Go to the file (starting from your Qt source directory), for versions higher than 2012, just use the right version everywhere (such as win32-msvc2015):

    • a. For VS2012: qtbase\mkspecs\win32-msvc2012\qmake.conf

    • b. For VS2010: qtbase\mkspecs\win32-msvc2010\qmake.conf

    • c. For Qt 5.5.0 and later (with any VS version): qtbase\mkspecs\common\msvc-desktop.conf

    and edit the following lines

     QMAKE_CFLAGS_RELEASE    = -O2 -MD
     QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
     QMAKE_CFLAGS_DEBUG      = -Zi -MDd
    

    to

     QMAKE_CFLAGS_RELEASE    = -O2 -MT
     QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi
     QMAKE_CFLAGS_DEBUG      = -Zi -MTd
    

Note: Qt 5.6+ have a configure parameter -static-runtime that will do this for you. You may not need to do this manually anymore for new versions of Qt.

  1. Start the terminal in linux, or in Windows, start the terminals of Visual Studio (which have the correct environment variables set, or alternatively use vcvarsall.bat). To start the command prompt and let it do this automatically for you, go to Start, All Programs:

    For Windows versions prior to 8: Find the Microsoft Visual Studio 201x folder, and launch the command prompt (either x86 for 32 bit or x64 for 64 bit).

    For Windows 8: go to Start, type “cmd” and all versions available for command prompt will show up. Choose the Visual Studio version appropriate (x86 for 32 bit or x64 for 64 bit).

Following is a screenshot of how it may look like. Always tend to select “Native” if it exists.

enter image description here

9.

  • For VS2012: Execute the following commands for VS2012

     set QMAKESPEC=win32-msvc2012
     set QTDIR=C:\Qt\Qt5.7\qtbase
     set PATH=C:\Qt\Qt5.7\qtbase\bin;%PATH%
    

Note: Setting QMAKESPEC environment variable is considered wrong for Qt versions 5.8+. Don’t do it for the new versions.

For dynamic linking (needs 8 GBs)

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop

For dynamic linking with no examples (needs 2 GB)

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -nomake examples -nomake tests

 Now the last command depends on what you want to compile. Just type configure -help and see what the available command-line parameters are.

For static linking (needs 70 GBs, yes it’s crazy, it’s more reasonable not to make the examples and demos).

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -static

For static linking with no examples (needs 4 GBs, makes more sense).

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -static -nomake examples -nomake tests

Now this will take a minute or two, then use jom as follows (assuming it’s extracted in C:\Qt\jom):

C:\Qt\jom\jom.exe -j 50

50 represents the number of cores you want to use. I use 50 because I have 8 threads and using only 8 will not occupy all cores completely, so more is better, but don’t get too greedy as it could make your system not responsive.

  • For VS2010: Execute the following commands for VS2010

    set QMAKESPEC=win32-msvc2010
    set QTDIR=C:\Qt\Qt5.7\qtbase
    set PATH=C:\Qt\Qt5.7\qtbase\bin;%PATH%
    

Note: Setting QMAKESPEC environment variable is considered wrong for Qt versions 5.8+. Don’t do it for the new versions.
For dynamic linking (needs 8 GBs)

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop

For dynamic linking with no examples (needs 2 GB)

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -nomake examples -nomake tests

The last command depends on what you want to compile. Just type configure -help and see what the available command-line parameters are.
For static linking (needs 70 GBs, yes it’s crazy, it’s more reasonable not to make the examples and demos).

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -static

For static linking with no examples (needs 4 GBs, makes more sense).

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -static -nomake examples -nomake tests

Now this will take a minute or two, then use jom as follows (assuming it’s extracted in C:\Qt\jom):

C:\Qt\jom\jom.exe -j 50

50 represents the number of cores you want to use. I use 50 because I have 8 threads and using only 8 will not occupy all cores completely, so more is better, but don’t get too greedy as it could make your system not responsive.

  • For linux:

There’s one small difference for Linux over Windows. It’s recommended in linux to install after compiling. Honestly this is the only way it works for me without problems.

Execute the following commands for Linux. Don’t forget to replace the paths with the correct paths of your Qt source

    export QMAKESPEC=linux-g++
    export QTDIR=/home/username/Qt5.7/qtbase
    export PATH=/home/username/Qt5.7/qtbase/bin:$PATH

Note: Setting QMAKESPEC environment variable is considered wrong for Qt versions 5.8+. Don’t do it for the new versions.

Let’s say you want to install the compiled source to the directory /home/username/Qt5.7-install. In this case, add the following to any of the configure commands below:

-prefix /home/username/Qt5.7-install

Warning: DO NOT install to the same source directory. That’s plain wrong!

If -prefix is not set, the default path will be chosen, which is /usr/local/ I guess. I don’t like to install anything using root. I always prefer installing in my user folder, so that reversibility and upgrades are not a problem.

The following are different possible configure commands depending on what you want to do.

For dynamic linking (needs 8 GBs)

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop

For dynamic linking with no examples (needs 2 GB)

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop -nomake examples -nomake tests

Now the last command depends on what you want to compile. Just type ./configure -help and see what the available command-line parameters are.

For static linking (needs 70 GBs, yes it’s crazy, it’s more reasonable not to make the examples and tests).

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop -static

For static linking with no examples (needs 4 GBs, makes more sense).

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop -static -nomake examples -nomake tests

After making is done, run make command

make -j 50

50 represents the number of cores you want to use. I use 50 because I have 8 threads and using only 8 will not occupy all cores completely, so more is better, but don’t get too greedy as it could make your system not responsive.

  1. Wait 2+ hours till the compilation is complete.

  2. Clean up! You can save a lot of space using this command for Windows: C:\Qt\jom\jom.exe clean And this command for linux: make clean

You can reduce the size of your compiled folder from 8 GB to 2.5 GB (for dynamic linking) and from 70 GB to 35 GB (for static linking).


In order to use this compiled version in Qt Creator:

  1. Start Qt Creator
  2. Go to Tools, Options
  3. Select Build and Run from the list on the left.
  4. Go to “Qt Versions” tab
  5. Click on “Add” and select qmake from the folder where your bin in
    qtbase is, so from above:

    C:\Qt\Qt5.7\qtbase\bin\qmake.exe

(or for Linux choose the path where you installed the compiled Qt source, which is equivalent to /home/username/Qt5.7-install/qtbase/bin/qmake in this tutorial)

  1. Click “Apply”
  2. Go to “Kits” tab
  3. Click “Add”
  4. Give it a name, choose the appropriate compiler (FOR VISUAL STUDIO
    EXPRESS DO NOT SELECT amd64 FOR 64-BIT , IT WON’T WORK, CHOOSE
    x86_amd64 INSTEAD)
  5. Click OK.

Now just open a project and you’ll find it asking you to choose the kit you added.

Enjoy 🙂

Leave a Comment