How to Build Qt From Source Codes

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.



Prerequisites

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:

https://download.qt.io/official_releases/qt/

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:

https://download.qt.io/official_releases/qt/5.9/5.9.2/single/qt-everywhere-opensource-src-5.9.2.zip

Or here:

https://download.qt.io/official_releases/qt/5.9/5.9.2/single/qt-everywhere-opensource-src-5.9.2.tar.xz

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

PATH_TO_EXTRACTED_QT_SOURCE_FILES/gnuwin32

Configuration

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:

cd PATH_TO_EXTRACTED_QT_SOURCE_FILES

And to start Qt configuration, type the following:

configure


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.

Configure summary:

...

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.

Configuration Examples

Below are some example configure commands that you can use for building Qt with various configurations:

Get configure help information:

configure -?

Standard build configuration (as we used in this tutorial):

configure

Auto select/confirm open-source license:

configure -opensource -confirm-license

Disable codes not allowed for app stores (all relevant platforms):

configure -appstore-compliant

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)

configure -platform-XXXXX

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 -MD
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -MD -Zi
QMAKE_CFLAGS_DEBUG = -Zi -MDd

And update them to the following:

QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MT
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -MT -Zi
QMAKE_CFLAGS_DEBUG = -Zi -MTd

And then run configure command as  mentioned above.

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:

http://doc.qt.io/qt-5/build-sources.html



18 Replies to “How to Build Qt From Source Codes”

  1. Hi Amin, Thanks for your awesome details for building qt from the source. Could you please guide me on how to build the installer framework from the source? I read all provided docs inside the installer framework source zip file. But I haven’t understood what I really need to do with dependencies, how can I configure the source? I really appreciate your help.

  2. Hi Amin,

    Thank you for the detailed steps to build Qt statically for windows, i did the same and builded successfully. Now i want to build it on Mac OS version 10.14. Kindly suggest the Qt version that could be suitable for mojave and build steps.

    Thanks & Regards,
    Sharief.

    1. The build steps mentioned in this post are universal and with little or no modifications you should be able to use them on Windows, macOS and Linux alike.
      Generally speaking, you need to have the dependencies (python, perl, bison, flex and so on) and run the following commands:

      ./configure
      make
      make install

      Obviously you can pass parameters like -prefix and so on to customize the configure command.
      The dependencies should be already there in macOS Mojave so usually you don’t need to lift a finger, and just run the commands I mentioned.
      Regarding the Qt version, as a rule, you can always go for the latest Qt version or if you want to be on the safe side, go for the latest LTS version of Qt which at the moment is 5.12.4 and it can be found in the following link:
      https://download.qt.io/official_releases/qt/5.12/5.12.4/

      1. Hi Amin,

        Thank you, i have build and generated the qmake with the above steps using Qt 5.12.4. But when am trying to use the genrated qmake from Qt build and try to generate binarycreator for QT_Installer_Framework 2.0.3 static build, facing the below errors

        usr/local/qt-everywhere-src-5.12.4/qtbase/bin/qmake
        Info: creating stash file /Users/Sharief/Build_QTIFW/qt-installer-framework-opensource-src-3.0.4/.qmake.stash
        $ make
        clang: error: unknown argument: ‘-mMacs-version-min=10.14’
        make[3]: *** [7zCrc.o] Error 1
        make[2]: *** [sub-7zip-make_first] Error 2
        make[1]: *** [sub-libs-make_first] Error 2
        make: *** [sub-src-make_first] Error 2

        Kindly provide some light to fix these errors.

        Thank you for your precious time,
        Sharief.

        1. What exactly are you trying to achieve?
          Do you want to build Qt Installer Framework from scratch?
          If that is the case, it has nothing to do with what’s described in this post.
          In order to build Qt Installer Framework from scratch, you need to refer to it’s source code read me files and documentations to figure out what’s missing.

          In any case, as a tip, you can simply download and use the binarycreator as part of the Qt Installer Framework from here:
          https://download.qt.io/official_releases/qt-installer-framework/3.1.1

          1. Hi Amin,

            Thank you for your time. Yes my goal is to build the QT Installer Framework from sources on Mac OSX 10.14 PC, So that i can create installer for my application.

            I have downloaded the pre built QT Installer Framework 3.1.1 .dmg for mac from the link provided by you and created the Installer for my application using its binarycreator binaries.

            But when i try to install this application installer on Mac Book air 10.14.5, it hangs while extracting the components. After waiting for so long time i did force quit. I did not understand what could be the problem and how to fix it.

            This hang is not happening all the time. on my next try i have successfully installed the application. the problem occur rate is 7/20 attempts.

            Kindly check the below errors from the problem report, and give your suggestion.


            Error Log
            =========
            Heaviest stack for the main thread of the target process:
            16 start + 1 (libdyld.dylib + 91093) [0x7fff57edd3d5]
            16 main + 5498 (testApp + 35738) [0x10010fb9a]
            16 InstallerBase::run() + 19660 (testApp + 63676) [0x1001168bc]
            16 QCoreApplication::exec() + 341 (testApp + 16533413) [0x1010cb7a5]
            16 QEventLoop::exec(QFlags) + 417 (testApp + 16515873) [0x1010c7321]
            16 QCocoaEventDispatcher::processEvents(QFlags) + 2191 (testApp + 6117519) [0x1006dc88f]
            16 -[NSApplication run] + 699 (AppKit + 79932) [0x7fff2958683c]
            16 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361 (AppKit + 104223) [0x7fff2958c71f]
            16 _DPSNextEvent + 965 (AppKit + 108935) [0x7fff2958d987]
            16 _BlockUntilNextEventMatchingListInModeWithFilter + 64 (HIToolbox + 42038) [0x7fff2b1f3436]
            16 ReceiveNextEventCommon + 603 (HIToolbox + 42661) [0x7fff2b1f36a5]
            16 RunCurrentEventLoopInMode + 292 (HIToolbox + 43371) [0x7fff2b1f396b]
            16 CFRunLoopRunSpecific + 459 (CoreFoundation + 236341) [0x7fff2bf14b35]
            16 __CFRunLoopRun + 1196 (CoreFoundation + 238131) [0x7fff2bf15233]
            16 __CFRunLoopDoSources0 + 195 (CoreFoundation + 240747) [0x7fff2bf15c6b]
            16 __CFRunLoopDoSource0 + 108 (CoreFoundation + 355681) [0x7fff2bf31d61]
            16 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 (CoreFoundation + 355771) [0x7fff2bf31dbb]
            16 QCocoaEventDispatcherPrivate::activateTimersSourceCallback(void*) + 18 (testApp + 6114434) [0x1006dbc82]
            16 QTimerInfoList::activateTimers() + 1192 (testApp + 16873400) [0x10111e7b8]
            16 QCoreApplication::notifyInternal2(QObject*, QEvent*) + 164 (testApp + 16531764) [0x1010cb134]
            16 SDKApp::notify(QObject*, QEvent*) + 14 (testApp + 74558) [0x10011933e]
            16 QApplication::notify(QObject*, QEvent*) + 510 (testApp + 7189454) [0x1007e23ce]
            16 QApplicationPrivate::notify_helper(QObject*, QEvent*) + 314 (testApp + 7184282) [0x1007e0f9a]
            16 QObject::event(QEvent*) + 258 (testApp + 16709458) [0x1010f6752]
            16 QSingleShotTimer::timerEvent(QTimerEvent*) + 155 (testApp + 16762571) [0x1011036cb]
            16 QMetaObject::activate(QObject*, int, int, void**) + 3113 (testApp + 16741497) [0x1010fe479]
            16 QInstaller::PackageManagerCore::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 1210 (testApp + 1534906) [0x10027dbba]
            16 QInstaller::PackageManagerCorePrivate::runInstaller() + 4549 (testApp + 552421) [0x10018dde5]
            16 QInstaller::PackageManagerCorePrivate::installComponent(QInstaller::Component*, double, bool) + 1453 (testApp + 563485) [0x10019091d]
            16 QInstaller::PackageManagerCorePrivate::performOperationThreaded(KDUpdater::UpdateOperation*, QInstaller::PackageManagerCorePrivate::OperationType) + 288 (testApp + 471472) [0x10017a1b0]
            16 QFutureInterfaceBase::waitForResult(int) + 222 (testApp + 14774302) [0x100f1e01e]
            16 QWaitCondition::wait(QMutex*, unsigned long) + 162 (testApp + 14820994) [0x100f29682]
            16 QWaitConditionPrivate::wait(unsigned long) + 75 (testApp + 14821323) [0x100f297cb]
            16 __psynch_cvwait + 10 (libsystem_kernel.dylib + 18538) [0x7fff5801586a]
            *16 psynch_cvcontinue + 0 (pthread + 19324) [0xffffff7f82d0ab7c]

            Thank you,
            Sharief.

          2. Check your installer scripts, try a simple installer. This looks like a deadlock and thread sync issue so there’s not much I can help you with.
            Try the simplest example installer out there and if you can get it to work, then start changing your script the way you need.

  3. Hi !
    I wonder if it’s possible to build Qt static using Visual Studio Code, instead of Community ?

    Thank you

    1. Hey Andres,
      Visual Studio Code or Community or any other IDE that you like, they’re just for convenience.
      You can use VS Code or even write your own set of scripts using Notepad and without using any professional code editor, as long as you have the necessary requirements for building Qt from sources.
      Using VS Community or Pro or whatever, they just guarantees that it’ll be easy to install the requirements (such as compiler and so on).

      1. I’m asking because VS Community, with the C++ package, is to heavy (~6GB) and the latest version (VS Community: 15.7.4) with Qt 5.11.2 is producing a weird assertion error (debug_heap.cpp -> __acrt_first_block == header) that I didn’t get before and I don’t get in Linux, with the same code, so I’m exploring other options in Windows. What would you recommend ?

        Thank you for your time.

        1. I see, well, why don’t you try MinGW?!
          It’s packed with the Qt installer so you don’t even need to download or install anything in addition.
          Just make sure the compiler and the kit are checked during the installation.

  4. Hi, thank you for the tutorial, is very helpful !
    I have one question. In Windows, Qt needs bison, flex and GPerf in PATH to build some libraries, so after installing Qt, can I delete the source files or there will be problems with the dependencies metioned above?

    Thank you

  5. I have successfully built the library for desktop using the -platform win32-msvc2017 flag with the x64 tools. Don’t really need WinRT builds for now.
    I need to deploy standalone executables without the need of shared libraries, that’s why I need to do a static build.

    1. Well my question was more like “why do you need static libs for WinRT?” Your deployed app will be a standalone package anyway (still talking about WinRT)
      In any case, I think you’ve found the answer to your question yourself anyway.

  6. Hi Amin, I am trying to compile the latest version, 5.9.4 using Visual Studio 2017 15.5.5 but the “nmake” command fails.

    Configuration: configure -platform winrt-x64-msvc2017 -opensource -confirm-license -static -skip webengine -prefix “C:\Qt\builds\5.9.4”

    I have uploaded the output of the “nmake” command to pastebin: https://pastebin.com/estX7cxc

    What can be the problem?

    Thanks in advance,
    William

    1. Hmmm, can you confirm that you are able to build with the same configuration but for Windows desktop platform (instead of WinRT which you are trying to build for?), since I haven’t really tried your configuration myself.
      On another note, and this is not exactly an answer to your question but more of a recommendation, why aren’t you just using dynamic and pre-built libraries? I can assure you the deployment is quite easy since all required Qt files and libraries get copied to the build folder anyway.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.