Skip to content

Building Qt v5.6 5.7 Statically on Windows

Albert Huang edited this page Dec 8, 2018 · 1 revision

Introduction

This guide provides instructions on how to build Qt v5.6. It's not too hard, but it's definitely cumbersome, with multiple people saying multiple things!

Why this guide? Why not another random guide on the magical World Wide Web/Web 2.0/Internet? Here's a couple of notes for those who like the nitty gritty details (especially if someone has dealt with building Qt before):

  • Visual Studio 2015 Community comes with many of the SDKs required, including DirectX SDK and some portions of Windows 10 SDK. Despite that, consensus indicates that the UCRT libraries are only available when installing it standalone, so that is reflected in my guide.
  • ICU is NOT enabled in this build. Qt developers have indicated that they've removed it due to the heaviness of ICU, and that seemingly not too many people rely on it. For my project, I didn't need ICU, so I didn't include it... but if you think ICU is important, feel free to contribute to this guide and I'll be happy to add it in!
  • Same with qtwebengine or qtwebkit - they are not enabled in this build due to the sheer size and dependencies! Again, if you have instructions on how to get those building, please contribute to this guide!
  • Thanks to some hard work from the Qt developers, you no longer have to do any hacks to get a static CRT working! The new -static-runtime option automatically configures the build to use the appropriate flags for a static CRT, e.g. /MT and/or /MTd.
  • The patch offered is from this issue, where QtQuick still requires the scripting folders in the same directory as the static EXE, just like the dynamic version. I combined that patch with modifications of my own to fix qtquickcontrols. I've only tested QtQuick.Controls, but given that the change made there was made with the other components of qtquickcontrols, I think it's safe to say that the fixes will do the trick! (If they fail, please let me know!)
  • For those who were building on Qt v5.6.0 beta, you may have noticed that XP builds broke. This was addressed and fixed with the release version of Qt v5.6.0 - no workarounds needed!
  • I use -opengl dynamic because this is what Qt is using since v5.5.
    • I had initial hesitations, but I think this is worth it because of significant variability in drivers. I've seen people with old graphics cards that simply don't work with Qt's OpenGL requirements.
    • With super old graphics cards, opengl32sw.dll is available to offer software rendering, which works pretty well in my tests. This is a good fallback for those who really have bad hardware!
    • -opengl angle is an option if you want to use DirectX 9+ instead. Again, I prefer to dynamically pick the best available rendering option, but this is 100% up to you!
  • Finally, through some extensive research + trial/error, I got OpenSSL to build statically on Windows! The commands listed below MUST be in order, and the paths must be set correctly for NASM/VC++ for it to run well.
    • The directories do get dirty (and irreversibly dirty) when configuration is performed, hence why I ask to create duplicate copies to ensure that each build is isolated.
    • The Windows-specific configuration must be run first before the NASM one. For whatever reason, it doesn't work the other way around...

As always, contributions are welcome (especially PRs)! Please contribute if you know how to fill in the rest!

Binaries

If you don't feel like building, binaries are available here: https://mega.nz/#F!fRE3DRQD!OxJcmIciB4PKhQo-kU4-bg

To install, simply extract everything to C:\. Everything will be generally available within C:\Qt. Note that you may need to setup your environment in order for building with the new Qt installation to work.

These builds include OpenSSL v1.0.2e and have been built with MSVC 2015. Remember that MSVC 2015 built libraries are generally only compatible with MSVC 2015 - linking with other versions of the C Runtime Library (CRT) can cause problems!

Guide

Building is as easy as 1, 2, 3! Except, of course, each step has sub-steps, just like college math problems...

  1. Download and Install Dependencies

    a. Compilers and SDKs

    • Visual Studio 2015 Community - a solid compiler and IDE for Windows. This is what we'll be using to build Qt.
      • Under Programming Languages, check Visual C++. Make sure that underneath Visual C++, Windows XP Support for C++ is checked as well. This will allow you to build a Qt that works under Windows XP.
    • Windows 10 SDK - the official SDK for Windows (but not necessarily for Windows 10).
      • Make sure that the Unified C Runtime library (CRT or UCRT) is installed.
      • You may have noticed that in Visual Studio 2015 Community, there is an option to install this SDK. Unfortunately, most steps point to installing this one instead, so I'm recommending this for now. If you managed to install Windows 10 SDK using VS2015, and still could compile this, let me know!
    • NASM - one of the many open source assemblers out there!
      • Download a build for 32-bit. You don't need 64-bit to assemble for 64-bit.
      • You may need to add the installation directory to your path manually - I don't think the installer offers to add it to PATH for you!
    • If you are having a hard time dealing with your PATH, consider using a tool like this!
      • In some cases, your PATH may become truncated if you have a LOT of stuff in your PATH - you can use this to recover!
      • Note that all paths specified above should be under the system PATH settings.

    b. Build Helper Tools

    • Python - a slithery programming language used to facilitate building Qt.
      • Make sure to download the 2.7.x version, and add it to your PATH during installation!
      • To verify that it installed correctly: open a fresh new Command Prompt, and type in python -V. If you see something that looks similar to Python 2.7.11, it works!
      • If it didn't work, you will need to add C:\Python27 to your PATH, or the location you installed Python to. No suffixes are needed beside the installation path - no \bin needed at the end!
    • Perl - a pearly programming language used to facilitate building Qt.
      • Make sure to download the 5.x version, and add it to your PATH during installation!
      • To verify that it installed correctly: open a fresh new Command Prompt, and type in perl --version. If you see This is perl 5.... followed by more text, it works!
      • If it didn't work, you will need to add C:\strawberry\perl\bin to your PATH, or the location you installed Perl to, followed by \bin.
    • Ruby - a sparkly programming language used to facilitate building Qt.
      • Make sure to download the latest version, and add it to your PATH during installation!
      • To verify that it installed correctly: open a fresh new Command Prompt, and type in ruby -v. If you see something that looks similar to ruby 2.2.1p85 (2015-02-26 revision 49769) [i386-mingw32], it works!
      • If it didn't work, you will need to add ;C:\Ruby22\bin to your PATH, or the location you installed Ruby to, followed by \bin.
    • patch - patch up your sorrows with this tool! You will need this tool to fix some issues with Qt regarding static builds.
      • You just need to download the binaries ZIP. Extract them somewhere, and set your PATH to point to the directory you extracted the patch tool to.
    • jom (OPTIONAL) - if you have multiple cores, and you want to use all of them, download this tool.
      • Extract this tool to a folder on your root drive, e.g. C:\Jom.
    • ConEmu (OPTIONAL) - not exactly a tool required, but it's a nicer alternative to using Command Prompt.
      • This is especially nice for those who appreciate PuTTY's shortcuts and features, as well as some Linux X11 terminal features too.
      • Nice features include: easy highlight + copy, easy right-click paste, tabs (yes, tabs!), native ANSI color support, background customization, transparency, etc.
      • You don't need this at all to build, but it's nice to have, especially if you have to work in the command line on Windows for the next day or so...

    c. Third-Party Libraries

    • OpenSSL - the famous (or even infamous!) open source security library.
      • I downloaded the 1.0.2 series, which worked fine.

    d. Qt Sources

    • Qt 5.6 - the magical library that you want to build!
      • Scroll all the way down to Source packages & Other releases. Download it as a ZIP file.
      • If you can't find it, make sure to click View All Downloads.
    • Qt 5.6 Static Building Scripts and Patches - scripts and patches to make life easier when building Qt!
      • The patch is to fix issues when using Qt v5.6 statically. This patch is for making Qt Quick and friends compile statically without having to include extra script directories!
  2. Build and Install Third-Party Libraries

    a. Build OpenSSL

    • Extract OpenSSL, if you haven't already.

    • Duplicate the folder into two new folders. Add a suffix -win32 to the first one, and the suffix -win64 to the second one.

      • For example: if openssl-1.0.2e was the original directory, I would duplicate that into openssl-1.0.2e-32bit and openssl-1.0.2e-64bit.
    • Open a fresh, new Command Prompt. (Or ConEmu... whatever you decided to go with!)

    • Initialize VS2015/x86 paths and environment variables:

      CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86

    • Enter the 32-bit directory you created, e.g.:

      cd openssl-1.0.2e-32bit

    • Run these next commands in order:

      perl Configure VC-WIN32 --prefix=C:\OpenSSL-win32
      ms\do_ms
      ms\do_nasm
      nmake -f ms\nt.mak
      nmake -f ms\nt.mak install
      
    • If everything goes well, those commands should complete with no errors! (There may be warnings, but they can be safely ignored.)

    • Close the current console and open a new one. That's right - close it! We need to change our environment to build the x64 version.

      • If you're using ConEmu, just open a new tab, then close the other one. Yay for tabs!
    • Initialize VS2015/x64 paths and environment variables:

      CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64

    • Enter the 64-bit directory you created, e.g.:

      cd openssl-1.0.2e-64bit

    • Note that the build commands differ slightly for x64. Run these next commands in order:

      perl Configure VC-WIN64A --prefix=C:\OpenSSL-win64
      ms\do_win64a
      ms\do_nasm
      nmake -f ms\nt.mak
      nmake -f ms\nt.mak install
      
      • Yes, that is actually the right command. Luckily for you stumbling on this page - I'm saving you hours and hours of insanity! Their codebase is pretty whacky... this is why LibreSSL was created to sort out the code (and security) mess!
        • (insert not-so-subtle comment to star this project if you find this useful!)
        • (insert even more not-so-subtle comment to follow this project if you want to hear more useful tidbits and updates I have!)
    • If everything goes well, those commands should complete with no errors! (There may be warnings, but they can be safely ignored.)

    • You've made it! But that's not all - now we get to build Qt!

  3. Build Qt

    a. Set up and Patch Qt Sources

    • Extract the Qt sources.

    • Copy the Qt sources to another directory, and rename it Qt56 or something short.

    • Check to see if the patch will apply:

      cd Qt56
      patch -p0 --dry-run < ..\path\to\Qt560-StaticFixBundle.patch
      
      • If anything fails (HUNKs are fine as long as they succeed), continue on!

        • Otherwise, try patching the code manually - usually it's just a few lines different that screws up the patch.
        • If the patch seems to have already been applied, the bugs could potentially be fixed!
      • If everything succeeds, go ahead and patch:

        patch -p0 < ..\path\to\Qt560-StaticFixBundle.patch

    b. Build Qt 32-bit

    • Open QtEnv32.bat and...

      • Change the paths inside to reflect your installation paths, and your Qt source directory.
        • Remember that the Qt source directory should be the duplicated and modified one, NOT the original!
    • Once you are satisfied with it, start a fresh new console, and run QtEnv32.bat:

      ..\path\to\QtEnv32.bat

    • Enter the Qt56 directory, if you haven't already.

    • Create a new directory called build_32, and enter it:

      md build_32

      cd build_32

    • Find BuildQt.bat and read it!

      • I'm specifying that we're using the open source LGPL license when building, and that I accept it. (Of course, you are the one accepting the license, so make sure you're fine with the LGPL before compiling!
        • Alternatively, if you're using the commercial version, modify the script do all of your licensing stuff.
      • I'm building both release and debug builds. One for production use, another for debugging purposes.
      • I'm enabling SSL/OpenSSL.
      • I'm setting the OpenGL configuration to dynamic. This is the default on Qt's builds, and provides a nice fallback when the user doesn't have good/recent OpenGL drivers OR DirectX drivers! (In some cases, they may have neither - VirtualBox, for instance!)
        • That said, you WILL need to redistribute libEGL / libGLESv2 / d3d* / opengl32sw since it's dynamic, despite the build being static.
        • If you don't want that, and want to take the risk (ick), you can specify -opengl angle instead to use DirectX 9+.
      • I'm specifying to build a static library, and make sure we're building with the static CRT. That way, we can be truly static and not have to redistribute the VS2015 support libraries.
      • I'm enabling XP support (via MSVC 2015's XP compatibility target). (I wish Microsoft wised up and offered free Windows 10 upgrades to XP users instead...)
      • I'm disabling compiling or even touching of Qt examples. We don't need them, and they will slow the build down and make it really big!
      • I'm specifying the OpenSSL libraries and include folders needed to make the static compile successful.
      • Finally, I'm specifying where Qt should be installed. I used the Qt folder format, but added -static to the end of the version number to indicate the build type.
      • Feel free to add/modify the configure options!
    • Once you are satisfied with the script, within the build_32 directory, run BuildQt.bat:

      ..\path\to\BuildQt.bat

    • The script is a bit of a misnomer - it just configures, and that's it! (I'll probably fix this soon...)

      • If you installed jom, build by running the command below, replacing # with the number of CPUs you have:

        jom -j #

      • If you did NOT install jom, simply build by running the command below:

        nmake

    • Once done, you can go ahead and install!

      • With jom:

        jom install

        • You could hypothetically add -j # before install, but I'm not sure if there's any dependency order to installing... let me know if using multicore to install still works!
      • Without jom:

        nmake install

      • Note that with both, there may be some errors. I think those are fine, but if you want to be sure, run the installation command again to ensure everything is installed!

        • Again, let me know if the first install is enough!
    • You did it! Note that unlike the regular Qt install, everything is layered in the prefix directory, e.g. C:\Qt\Qt5.6.0-static\bin\qmake instead of the subdirectories in the regular install. Make sure to take a note of that!

      • Again, if you have a way to structure this like Qt does, let me know!

    c. Build Qt 64-bit

    • Open QtEnv64.bat and...

      • Change the paths inside to reflect your installation paths, and your Qt source directory.
        • Remember that the Qt source directory should be the duplicated and modified one, NOT the original!
        • Note that we are building for 64-bit now. That said, QMAKESPEC will stay the same - the compiler makes the difference!
    • Once you are satisfied with it, start a fresh new console, and run QtEnv64.bat:

      ..\path\to\QtEnv64.bat

    • Enter the Qt56 directory, if you haven't already.

    • Create a new directory called build_64, and enter it:

      md build_64

      cd build_64

    • Find BuildQtx64.bat and read it!

      • I'm specifying that we're using the open source LGPL license when building, and that I accept it. (Of course, you are the one accepting the license, so make sure you're fine with the LGPL before compiling!
        • Alternatively, if you're using the commercial version, modify the script do all of your licensing stuff.
      • I'm building both release and debug builds. One for production use, another for debugging purposes.
      • I'm enabling SSL/OpenSSL.
      • I'm setting the OpenGL configuration to dynamic. This is the default on Qt's builds, and provides a nice fallback when the user doesn't have good/recent OpenGL drivers OR DirectX drivers! (In some cases, they may have neither - VirtualBox, for instance!)
        • That said, you WILL need to redistribute libEGL / libGLESv2 / d3d* / opengl32sw since it's dynamic, despite the build being static.
        • If you don't want that, and want to take the risk (ick), you can specify -opengl angle instead to use DirectX 9+.
      • I'm specifying to build a static library, and make sure we're building with the static CRT. That way, we can be truly static and not have to redistribute the VS2015 support libraries.
      • I'm enabling XP support (via MSVC 2015's XP compatibility target). (I wish Microsoft wised up and offered free Windows 10 upgrades to XP users instead...)
      • I'm disabling compiling or even touching of Qt examples. We don't need them, and they will slow the build down and make it really big!
      • I'm specifying the OpenSSL libraries and include folders needed to make the static compile successful.
        • This time, I'm using the x64 libraries for the x64 build.
      • Finally, I'm specifying where Qt should be installed. I used the Qt folder format, but added -static to the end of the version number to indicate the build type. I also use x64 as my standard to indicate the bits of the build.
      • Feel free to add/modify the configure options!
    • Once you are satisfied with the script, within the build_64 directory, run BuildQtx64.bat:

      ..\path\to\BuildQtx64.bat

    • The script is a bit of a misnomer - it just configures, and that's it! (I'll probably fix this soon...)

      • If you installed jom, build by running the command below, replacing # with the number of CPUs you have:

        jom -j #

      • If you did NOT install jom, simply build by running the command below:

        nmake

    • Once done, you can go ahead and install!

      • With jom:

        jom install

        • You could hypothetically add -j # before install, but I'm not sure if there's any dependency order to installing... let me know if using multicore to install still works!
      • Without jom:

        nmake install

      • Note that with both, there may be some errors. I think those are fine, but if you want to be sure, run the installation command again to ensure everything is installed!

        • Again, let me know if the first install is enough!
    • You did it! Note that unlike the regular Qt install, everything is layered in the prefix directory, e.g. C:\Qt\Qt5.6.0x64-static\bin\qmake instead of the subdirectories in the regular install. Make sure to take a note of that!

      • Again, if you have a way to structure this like Qt does, let me know!

Updates

The following are update notes for this guide - please read them if you can, since they may solve your problem!

UPDATE #1: (9/25/16)

All scripts and patches should now be on the repo! If there are any comments/issues, please let me know! If you have any changes to the guide, scripts, or patches, feel free to create a pull request as well! Finally, a "Binaries" section has been added for those who don't feel like following these steps - right under Introduction!

UPDATE #2: (3/21/17)

Although these instructions may work for Qt v5.7, they do NOT work for Qt v5.8! With Qt v5.8, they have overhauled their configure tool to become more powerful and flexible! However, this means that a LOT of their configuration tooling and options have changed as a result. Qt v5.8 guide coming soon - thanks to @davidm71 for the report (issue #4)!

UPDATE #3: (3/22/17)

Random tidbit: MSVC Qt currently uses OpenSSL, which has an awful build process. LibreSSL has a much cleaner, secure codebase, and a much better build system to boot. However, it isn't able to build with Qt due to some missing APIs (Qt bug). LibreSSL fixed this recently (relevant issue), and around 8/1 should have a new release out with this fix. Assuming everything works, I'll probably be switching to LibreSSL to embed from that point on...

UPDATE #4: (3/22/17)

I tried to build Qt v5.8, but ended up crashing the MSVC compiler! Output below follows:

Microsoft (R) Program Maintenance Utility Version 14.00.23506.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f Makefile.Release

Microsoft (R) Program Maintenance Utility Version 14.00.23506.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        cl -c -nologo -Zc:wchar_t -FS -Zc:strictStrings -Zc:throwingNew -O2  -MT -D_HAS_EXCEPTIONS=0 -GR -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -DUNICODE -DWIN32 -DQT_VERSION_STR="\"5.8.0\"" -DQT_VERSION_MAJOR=5 -DQT_VERSION_MINOR=8 -DQT_VERSION_PATCH=0 -DQT_BOOTSTRAPPED -DQT_NO_CAST_TO_ASCII -DQT_NO_FOREACH -DQT_NO_CAST_FROM_ASCII -DQT_BUILD_BOOTSTRAP_LIB -DQT_BUILDING_QT -D_CRT_SECURE_NO_WARNINGS -D_USE_MATH_DEFINES -DQT_ASCII_CAST_WARNINGS -DQT_MOC_COMPAT -DQT_USE_QSTRINGBUILDER -DQT_DEPRECATED_WARNINGS -DQT_DISABLE_DEPRECATED_BEFORE=0x040800 -DQT_NO_EXCEPTIONS -DQT_NO_DEBUG -DNDEBUG -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\src\tools\bootstrap -I. -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\src\3rdparty\zlib -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include\QtCore -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include\QtCore\5.8.0 -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include\QtCore\5.8.0\QtCore -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include\QtXml -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include\QtXml\5.8.0 -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\include\QtXml\5.8.0\QtXml -I..\..\..\include -I..\..\..\include\QtCore -IC:/Users/Win7x64/Documents/Qt58/Qt58Build32/qtbase/include/QtXml -Itmp -IC:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\mkspecs\win32-msvc2015  -Fo.obj\release\ @C:\Users\Win7x64\AppData\Local\Temp\nmD216.tmp
qlogging.cpp
C:\Users\Win7x64\Documents\Qt58\qt-everywhere-opensource-src-5.8.0\qtbase\src\corelib\global\qlogging.cpp : fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 246)
 To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
 Help menu, or open the Technical Support help file for more information

INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.EXE'
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.EXE"' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.

This seems to be related to this issue, where VS2015 Update 1 caused some issues with building projects. At the time of writing, Update 3 was available, and upgrading to that seems to have fixed this problem!