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:
Download and install Perl: Download link
Download and install Python: Download link
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.
Download and install DirectX SDK (probably necessary if you wanna compile with OpenGL) Download link
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
Download Qt Opensource, and extract it, say to
C:\Qt\Qt5.6
, so now the folderqtbase
can be found inC:\Qt\Qt5.6\qtbase
.-
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.
-
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.
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.
Wait 2+ hours till the compilation is complete.
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:
- Start Qt Creator
- Go to Tools, Options
- Select Build and Run from the list on the left.
- Go to "Qt Versions" tab
-
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)
- Click "Apply"
- Go to "Kits" tab
- Click "Add"
- 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)
- Click OK.
Now just open a project and you'll find it asking you to choose the kit you added.
Enjoy :)
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, 2020Comments
-
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 over 10 yearsIt 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 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 over 10 yearsGreat 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 over 10 years@user929404 Thanks for the tip. I'll try to make it clearer :)
-
prakashpun about 9 yearsHi, 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 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 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 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 about 9 yearsHi @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 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 about 9 yearsHi @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 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 over 8 yearsI 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 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 over 8 yearsOkay, 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 over 8 yearsWhat build flags do I need to pass to make it 32 bit in Linux/OS X?
-
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 over 8 yearsUmm, 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 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 over 8 yearsBuild 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 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 over 8 yearsI 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 over 8 yearsOkay. Got it.
./configure -platform macx-clang-32
. This will need XCode, of course. Source - doc.qt.io/qt-5/osx.html -
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 over 8 yearsThanks. By the way, this is a gem of an answer!
-
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 tog++
. -
The Quantum Physicist over 8 years@AttitudeMonger Sorry it's
-platform macx-g++-32
for 32 bit (forgot the-32
). Please go to that directoryqtbase/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 over 8 yearsFYI, the
make clean
step on OS X at least takes more time thanmake -j 50
itself.. :) -
The Quantum Physicist over 8 years@AttitudeMonger I think you can parallelize
make clean
usingmake -j 50 clean
ormake clean -j 50
. This will parallelize the cleaning and make it much faster. -
SexyBeast over 8 yearsAh. Was thinking the same.. :) Same goes for
make install
I assume? In my case, themake -j50
was successful. Then I ranmake clean
and it was going on for ages, so I stopped it midway, and then ranmake install
. I got a build error stating some header file is missing. May be I should runclean
only after install. So I am doing everything afresh again.. By the way, I did runexport QTDIR=/home/username/Qt5.2/qtbase
. But even thenmake install
was installing everything in/usr/local/
... -
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 themake 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 yourconfigure
. 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 over 8 yearsPlease 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 over 8 years@AttitudeMonger Done!
-
SexyBeast over 8 yearsI think adding the OS X instructions will complete the answer big time. Mind if I edit and add my entire experience?
-
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 over 8 yearsYep. Totally. Will do.
-
kayleeFrye_onDeck over 7 yearsCircling 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 over 7 years@kayleeFrye_onDeck I'm sorry, I don't see a question. How can I help you?
-
kayleeFrye_onDeck over 7 yearsNo 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 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 over 7 yearsA 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 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 over 7 yearsDid you compile all of qt as static, or just qtbase?
-
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 over 7 yearsAll of qt, or just the qtbase?
-
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 over 7 yearsThere'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 useqtbase
in your examples. I was asking if you ever compile all of Qt, or just inside theqtbase
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 buildingqtbase
by itself statically. -
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 over 7 yearsLet 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 over 7 years@kayleeFrye_onDeck I see.
-
guilhermecgs over 6 yearsDo I need to run nmake install after nmake, in Windows?
-
guilhermecgs over 6 yearsI 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 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 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 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 over 6 years@guilherm Thanks for the info. I'll be trying that some time soon.