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

55,301

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 :)

Share:
55,301
The Quantum Physicist
Author by

The Quantum Physicist

I did a PhD of particle physics back in the days, and then left academia to start a professional career with software development, because I love it. I still contribute to science by leading software development efforts for scientific experiments. I love science, but I love programming even more :-)

Updated on October 22, 2020

Comments

  • The Quantum Physicist
    The Quantum Physicist over 3 years

    Just a post to help those guys trying to do that, since I don't have a blog.

    This works for linux too. Feel free to edit it and improve it.

  • rwx
    rwx over 10 years
    It works!! Thank you! Excellent post. One small correction that I did, I had to put jom.exe in the same directory as configure.bat otherwise jom will complain about not being able to find a Makefile. So to be precise, I copied ibjom.cmd, jom.exe and profile.xml into the Qt source directory.
  • The Quantum Physicist
    The Quantum Physicist over 10 years
    @rwx Actually the solution I prefer is that you put jom in some other directory (say C:\jom), and then have your command-line prompt be in the directory where configure.bat is located. Then run jom with the full command C:\jom\jom.exe, and not only jom.exe, and so you won't need to have a copy of jom in your configure.bat dir :)
  • user929404
    user929404 over 10 years
    Great guide! Thank you. Just a minor gripe: When I read "DO YOU REALLY WANT IT TOTALLY STATIC?", I thought that what followed were instructions to NOT compile Qt statically.
  • The Quantum Physicist
    The Quantum Physicist over 10 years
    @user929404 Thanks for the tip. I'll try to make it clearer :)
  • prakashpun
    prakashpun about 9 years
    Hi, Thank you so much for this useful tuts. It was really helpful. I was able to compile the code statically, but when i use it in the Qt creator i get error saying: error: C1041: cannot open program database 'd:\qt\rxyz.pdb'; if multiple CL.EXE write to the same .PDB file, please use /FS. Is there anything else i need to do ? Any inputs will be appreciated
  • The Quantum Physicist
    The Quantum Physicist about 9 years
    @pra16 I remember this happening as a warning not as an error. I think this happens due to the clean command. Try to compile without cleaning and probably this should go away. I'm not sure though.
  • prakashpun
    prakashpun about 9 years
    @The Quantum Physicist I re-installed VS2013 and after that I am getting this error: LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1800' in main.obj. Does this mean static compilation was a failure?
  • The Quantum Physicist
    The Quantum Physicist about 9 years
    @pra16 Not sure what that means. So sorry... If you get the answer eventually, let me know so that I can add it here for the benefit of others. Thanks.
  • prakashpun
    prakashpun about 9 years
    Hi @The Quantum Physicist, I really appreciate the efforts you have taken to comment to my queries. After re-compiling Qt source, when i run the project in Qt creator, I now get this error. if multiple CL.EXE write to the same .PDB file, please use /FS What can be done for this ? Thanks
  • The Quantum Physicist
    The Quantum Physicist about 9 years
    @pra16 I'm so sorry. I have no idea what that error is. Try to ask on stackoverflow and refer to this tutorial. I compiled Qt a billion times using this tutorial and never got to any problems. Regards.
  • prakashpun
    prakashpun about 9 years
    Hi @The Quantum Physicist. I Tried rebuilding the Qt source again. This time i used msvc2012. I think i made a huge blunder of providing win32-msvc2013 with configure command :| Its a real stupid mistake from my side. Anyway, thank you so much for the tutorial. It was really helpful :)
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @kaylee Have you tried the same steps but with replacing every 2013 with 2015? I haven't tried it myself, but I will soon.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 8 years
    I will try that, but as a caveat, I believe Qt is not officially supporting 2015 until 5.6 -- I've never built Qt before, only deployed it on to build agents, so I was mostly wondering if there was anything new in regards to potential snags. I'll share my results with you.
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @kayleeFrye_onDeck In principle there's nothing different, except a minor thing related to Qt 5.5.0, which I'll edit now about creating fully static builds. Thanks for willing to share.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 8 years
    Okay, all went well, but your steps had to be changed up a little bit, so I went ahead and posted an answer on here, but here's the link anyway: stackoverflow.com/a/31886054/3543437
  • SexyBeast
    SexyBeast over 8 years
    What build flags do I need to pass to make it 32 bit in Linux/OS X?
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger It's included in the answer for both static and non-static linking. Search this page for the word "For linux".
  • SexyBeast
    SexyBeast over 8 years
    Umm, I did. All I saw is that I should use ./configure -help to find out all possible options. Is that what you are talking about? The 32 bit examples are all Windows specific..
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger The ./configure is all the options you need for compiling. In linux you cannot choose to compile 32-bit (AFAIK), if that's what you're asking. Your operating system will choose based on your compiler and your architecture (unlike Windows). What is it your trying to do exactly?
  • SexyBeast
    SexyBeast over 8 years
    Build 32 bit Qt on OS X and Windows. In OS X there definitely is. I have built tons of library, like OpenSSL, Poco etc for 32 bit on OS X...
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger I'm sorry I have zero experience on OSX. I work only on Windows and Linux. But I think this has nothing to do with the steps I showed, because it depends on your compiler. On Windows I show how to command Visual Studio to use the 32-bit compiler in the steps. If your compiler produces 32 bit binaries, then the same steps should work. That's how it works for Windows and Linux AFAIK.
  • SexyBeast
    SexyBeast over 8 years
    I am still not able to understand what do you mean by whether compiler is able to. It is g++, depending on the flags we pass, it will build 32 bit or 64 bit binaries. You mentioned the flags for Windows - win32-msvc2010. What is it for Linux? g++ can produce both, but what it will depends on what we pass to it, right?
  • SexyBeast
    SexyBeast over 8 years
    Okay. Got it. ./configure -platform macx-clang-32. This will need XCode, of course. Source - doc.qt.io/qt-5/osx.html
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger I'm sorry I didn't get that that's your question. Anyway, you can know all possible -platform values by going to your Qt source, then in this directory: qtbase/mkspecs/. You'll find there a full list of all values. Good luck!
  • SexyBeast
    SexyBeast over 8 years
    Thanks. By the way, this is a gem of an answer!
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger Glad to help! By the way, I just checked and found you can use -platform macx-g++, too. This seems to be specific to g++.
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger Sorry it's -platform macx-g++-32 for 32 bit (forgot the -32). Please go to that directory qtbase/mkspecs/ and check what possibilities you have. I know it hurts when it gives a compilation error. So I'm trying to not let that happen. Good luck!
  • SexyBeast
    SexyBeast over 8 years
    FYI, the make clean step on OS X at least takes more time than make -j 50 itself.. :)
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger I think you can parallelize make clean using make -j 50 clean or make clean -j 50. This will parallelize the cleaning and make it much faster.
  • SexyBeast
    SexyBeast over 8 years
    Ah. Was thinking the same.. :) Same goes for make install I assume? In my case, the make -j50 was successful. Then I ran make clean and it was going on for ages, so I stopped it midway, and then ran make install. I got a build error stating some header file is missing. May be I should run clean only after install. So I am doing everything afresh again.. By the way, I did run export QTDIR=/home/username/Qt5.2/qtbase. But even then make install was installing everything in /usr/local/...
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger Yes, but I don't do that for make install. I don't trust it because I want the installation to be clean. I would do the make install before I clean. To install stuff to a specific directory you have to use the command line -prefix /home/username/Qt5.2-install with your configure. DO NOT install in the same source directory. That's wrong! QTDIR defines the source, not the installation directory. make install is not necessary for Windows. Maybe I should add this information to the answer.
  • SexyBeast
    SexyBeast over 8 years
    Please mention the prefix part in the answer! Without that, Qt does not let user choose from file browser the qmake in /usr/local/ !
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger Done!
  • SexyBeast
    SexyBeast over 8 years
    I think adding the OS X instructions will complete the answer big time. Mind if I edit and add my entire experience?
  • The Quantum Physicist
    The Quantum Physicist over 8 years
    @AttitudeMonger Oh, absolutely! Please feel free to do that. But please make it clear and in the same syntax so that I won't have to edit it myself, since I don't use mac.
  • SexyBeast
    SexyBeast over 8 years
    Yep. Totally. Will do.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    Circling back. In Qt 5.6.1-1 I can't seem to statically build using the instructions on a Win10 box for all of qt, not just qtbase. Dynamic seems to be working fine.
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck I'm sorry, I don't see a question. How can I help you?
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    No question, just a caveat that the instructions as-written aren't compatible for later versions of qt. Your instructions worked perfectly for dynamic though :)
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck I compiled 5.7 like a month ago with no problems. Maybe you should remove a keyword or something from here or there. I hope you're not just copy/pasting everything without being a little creative. After all, you're a developer, and software development is art ;-)
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    A caveat is a caveat. I'm not trying to piss on your answer, so don't take it personal. It's not like I downvoted you or anything... So, you compiled qtbase or all of qt? I followed your instructions, and they worked for everything short of static for all of qt as opposed to just qtbase. The only potential diff I see is that you don't document compatibility of Windows beyond 8. I made sure to use distclean between compilations as well.
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck Dude... I'm not offended on any level, neither personal nor professional. I'm just saying that it did work with me and I use these instructions myself all the time, and I use Qt since years. (btw, I use Windows 10 since it came up). If you have a specific error where you need help, I'll try to help. Otherwise, have a nice day :)
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    Did you compile all of qt as static, or just qtbase?
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck I compile Qt exclusively statically. Never had to deal with dynamic linking (except on Linux). One doesn't need to compile a dynamic version on Windows except for learning purposes. The dynamic version supports plugins, which you can always add later.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    All of qt, or just the qtbase?
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck I don't even know what "just qtbase" means. What I do is in the tutorial.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    There's the Qt base, as in from your tutorial, C:\Qt\Qt5.6\qtbase. C:\Qt\Qt5.6 would be all of qt. You only use qtbase in your examples. I was asking if you ever compile all of Qt, or just inside the qtbase folder. I will assume that you only build qtbase, from what I've seen, but I figured I would ask since it was what I couldn't build whereas I didn't have a problem building qtbase by itself statically.
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck I don't see the difference. I use all of Qt parts, including GUI, data structures, Networking, OpenGL and others statically. Practically, I build single executables on Windows. Anyway... Have a nice day :)
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    Let me explain it for you: qtbase == qt stuff; one directory up is qtbase + qt dependencies. So, for example, chromium is one of those things that would be built. I hope that makes more sense for you.
  • The Quantum Physicist
    The Quantum Physicist over 7 years
    @kayleeFrye_onDeck I see.
  • guilhermecgs
    guilhermecgs over 6 years
    Do I need to run nmake install after nmake, in Windows?
  • guilhermecgs
    guilhermecgs over 6 years
    I compiled everything, but inside the "install_folder\qtbase\include\" there is NO *.h files.... Then I saw this post forum.qt.io/topic/35039/… ... It says that nmake install is necessary for windows
  • The Quantum Physicist
    The Quantum Physicist over 6 years
    @guilhermecgs I never had to install on Windows. I compiled Qt 5.7.1 a few days ago without an issue. The way I get it to work after compiling is that I add the compiled Qt directory to Qt Creator through its qmake.exe, and then it deals with header includes.
  • guilhermecgs
    guilhermecgs over 6 years
    @TheQuantumPhysicist, ok, thanks... I am using VisualStudioExpress as IDE, so I need to check how to do something similar ... Since the express version does not have QTAddon, it will be a more painful. The only thing I am sure is that inside the "/folder/include/QtGui" it has only a QtGuiDepends file (no QtGui.h) If you look in the official version from download.qt.io, it has
  • guilhermecgs
    guilhermecgs over 6 years
    @TheQuantumPhysicist, i have tested inserting a "-prefix folder" parameter and nmake install on windows, just as described in the link below... It worked as a charm...
  • The Quantum Physicist
    The Quantum Physicist over 6 years
    @guilherm Thanks for the info. I'll be trying that some time soon.