In this post you’ll find information about how to build Qt from source. We’ll be building the latest version of Qt, released about a week ago, using its source codes, both dynamically and statically and with various customized configurations. So without further ado, let’s start.
Assumptions (means Prerequisites) for this process are the following:
- On Windows this is Microsoft Visual Studio
- On macOS this is XCode and relevant development tools.
- On Linux, this is Build essentials and relevant development tools.
- On all platforms you need to have Python installed (2.7 or later, NOT 3.X because some modules, such as Web Engine simply do not support it at the moment) Check this by running “python -h” command from command prompt or terminal. If you don’t have it installed you won’t see it’s help messages, so just download and install it. If it’s simply not in the path, make sure you add it.
- On all platforms you need to have ActivePerl installed. (Check this by running “perl -h” command from command prompt or terminal. If you don’t have it installed you won’t see it’s help messages, so just download and install it. If it’s simply not in the path, make sure you add it.)
- You also need bison, flex and GPerf in order to successfully build some modules such as the QtWebEngine module. On Windows, they are included in the gnuwin32/bin folder inside the source files folder for convenience purposes. Just make sure they are added to the PATH.
Make sure to get the latest version of each one of dependencies mentioned above, apart from the ones which are explicitly mentioned. If you don’t have the latest version, then just update it.
If you are sure about the prerequisites mentioned above, then just proceed with the instructions. In any case, even if something is missing you’ll be faced with (usually meaningful) errors and warning messages which will lead you back to the missing (not installed) prerequisites.
Get the source files
OK, to start, first of all, download the latest Qt 5.9.2 source codes from here:
Just so that we have an example case during this tutorial, I will assume 5.9.2 is the latest version of Qt, so we can get it from here:
Extract it to a folder of your choice (let’s call it “PATH_TO_EXTRACTED_QT_SOURCE_FILES”)
As it was mentioned in the Prerequisites section, if you are on Windows, you must add the following to the PATH (This is only required in Windows for taking care of bison, flex and gperf binaries):
We are ready to configure our Qt build:
- If you are on Windows, open “Developer Command Prompt for VS ####” (replace “####” with 2017, 2015 and so on depending on your installed MSVC version) in Administrator mode
- If you are on macOS or Linux, simply open up a terminal instance.
After that, switch to the folder where you extracted the source files from the link above, using the command below:
And to start Qt configuration, type the following:
Wait until you are presented with the following:
Selecting Qt Edition.
Type ‘c’ if you want to use the Commercial Edition.
Type ‘o’ if you want to use the Open Source Edition.
Which edition of Qt do you want to use?
Simply, type “o” (for Open Source Edition) and press Enter.
After a few moments, you should be getting the following prompt:
You are licensed to use this software under the terms of
the GNU Lesser General Public License (LGPL) version 3
or the GNU General Public License (GPL) version 2.
Type ‘L’ to view the GNU Lesser General Public License version 3 (LGPLv3).
Type ‘G’ to view the GNU General Public License version 2 (GPLv2).
Type ‘y’ to accept this license offer.
Type ‘n’ to decline this license offer.
Do you accept the terms of either license?
Again, simply type “y” (for Yes) and press Enter.
This will start the configuration tests, you’ll see lots of informational text similar to the following:
Running configuration tests…
Done running configuration tests.
WARNING: Python version 2 (2.7.5 or later) is required to build QtWebEngine.
Qt is now configured for building. Just run ‘nmake’.
Once everything is built, you must run ‘nmake install’.
Qt will be installed into ‘\usr\local\Qt-5.9.2’.
Prior to reconfiguration, make sure you remove any leftovers from
the previous build.
Build and Install
Type “nmake” if you are on Window, or “sudo make” if you are on macOS or Linux.
Note that on Windows, if you have “jom” installed you can replace “nmake” with “jom”. As a tip, you can usually find “jom” in “bin” folder of Qt Creator. “jom” can help
Then, wait for a long (long, long) time until building is finished.
- If you face any issues and you have to rebuild from scratch, make sure you clean the build folder using “nmake clean” command on Window, and “make clean” command on macOS and Linux. After fixing the issues one way or another, start from the “make” step above, you don’t have to re-configure if you will go with the same configuration.
- Otherwise, meaning if everything went fine during the build,
- And if you provided a “-prefix” parameter to “configure” command (see example configurations below), then just type “nmake install” if you are on Windows, otherwise “sudo make install”, then press Enter to get your Qt built files in the folder of your preference.
- If you didn’t provide a “-prefix” parameter to “configure” command, by default Qt uses the folder you saw in the messages at the end of configuration, which was “\usr\local\Qt-5.9.2” in our case. So typing “nmake install” (or “sudo make install”) will install the libraries in the default folder.
Below are some example configure commands that you can use for building Qt with various configurations:
Get configure help information:
Standard build configuration (as we used in this tutorial):
Auto select/confirm open-source license:
configure -opensource -confirm-license
Disable codes not allowed for app stores (all relevant platforms):
Explicitly mentioning platform (See PATH_TO_EXTRACTED_QT_SOURCE_FILES/qtbase/mkspecs. Replace XXXXX below with an entry in mkspecs folder, such as winrt-x64-msvc2017)
Static build, the recommended way (since QtWebEngine does not support static linking at the moment):
configure -opensource -confirm-license -static -skip webengine
Skipping any Qt repository you don’t need (Replace XXXXX with the name of repository in PATH_TO_EXTRACTED_QT_SOURCE_FILES. Omit the initial “qt” from the repository name):
configure -skip XXXXX
Changing the output (install) directory of the created Qt build:
configure -prefix “C:\Qt\build”
You can use any combination of the parameters to further customize your Qt build as you require.
When building Qt statically for Windows (using MSVC Compilers), you’ll still need to have MSVC Runtime DLL files. To remove dependency on MSVC Runtime DLL files (AKA MSVC Redistributables) you need to tweak msvc-desktop.conf file (Located in PATH_TO_EXTRACTED_QT_SOURCE_FILES/qtbase/mkspecs/common). Look for the following codes in msvc-desktop.config file:
QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MDQMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -MD -ZiQMAKE_CFLAGS_DEBUG = -Zi -MDd
QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MTQMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -MT -ZiQMAKE_CFLAGS_DEBUG = -Zi -MTd
As always, put a comment below if you’ve got any questions about building Qt from source codes. You can also refer to the following link for a complete list of requirements and possible build customization on all platforms: