Building static Qt 5

This is a detail guilde how to build the static version of the Qt framework. Static build of Qt allows you to build solid applications which will work without lots of additional *.DLL/*.SO or *.DYLIB files because everything will be hardcoded into your application.

Introdution
Static version of Qt allows you to build solid application files without packing of a lots of dependent libraries.

'' Don't forget that with the static version of Qt, you can only legally release your Open-Source applications! (With GPLv3 license). If you want to use the static Qt build in commercial development for closed-source applications, then you should buy the commercial license for that purpose. ''

Install dependencies
Before start build you must install all necessary dependencies:

Install on Debian/Ubuntu/Mint

Install on Mac OS X (before start you must have pre-installed latest version of XCode)

Via homebrew

Via MacPorts

Download sources
At first, you must to download entire source code tarball from official Qt site:

http://download.qt.io/official_releases/qt/5.8/5.8.0/single/qt-everywhere-opensource-src-5.8.0.tar.gz

Note: if this link is outdated, you can find newer version of Qt sources here:

Qt open-source Download page

Unpack archive
Unpack everything from the tarball into any folder (on Linux or on Mac OS X):

Remove a glitchy JPEG2000 library (for Qt 5.5.1 and older)
For Qt 5.6 and higher this step must be skipped because JPEG2000 and MNG since Qt 5.6 are already disabled by default for security reasons.

This library provides a support of JPEG2000 image format support, but in the PGE it is useless (main image formats in the PGE are PNG, GIF and BMP), and also causes a random crashes of Qt applications built with your Qt build.

To resolve next issues, recommended to remove building of JPE support: because disabling of JPE building is not provided by ./configure script given with Qt sources, we must remove building of JPE weself:
 * make a kill_jasper.sh file in parent folder of Qt sources folder
 * on OS X: install via homebrew the coreutils and gnu-sed
 * open a text editor ( On OS X: Don't use TextEditor.app, because it makes invalid code!, use text editor from XCode or use nano) and paste next content into it:
 * open command line and execute this script by "bash ./kill_jasper.sh" command

Same for OSX with gnu-sed:

Configure
Open console then change current directory to new folder which you has been unpacked. then Copy-paste this into console ans press ENTER:

Linux Mint / Debian (for Qt 5.8 and higher)

Linux Mint / Debian (for Qt 5.7 and Qt 5.7.X)

Linux Mint / Debian (for Qt 5.6 and lower)

Mac OS X (for Qt 5.7 and Qt 5.7.X)

then wait while configuring will be finished (you will need to wait ~30...60 min)

Build and install
(you will wait ~1.2...2 hours)

Hint: you can speed-up building process with adding -r argument, and adding -j x (where x - number of concurrent jobs. For example -j 4)

Final step is installation which will copy all built components into target installation directory (you will wait ~10..30 min)

Usage
Built Qt will be located at ~/Qt/5.5.1_static or ~/Qt/5.5.1_static_osx on Mac OS X. To configure your Qt application to be built with static Qt version, you need to call ~/Qt/5.5.1_static/bin/qmake while you in your project folder, and other steps are same as you build with regular Qt version.

If you wants to plug static build of Qt into Qt Creator, just make a new toolchain with your default compiler (GCC, CLang or MinGW on Windows systems) and debugger, and with your static build of Qt (find QMake in the bin subdirectory of your static Qt build)

Note: Carefully transfer packages with a static build of Qt, you should keep same absolute path (because it is hardcoded) if you don't want to rebuild Qt again.

Download and install dependencies
Before start building you must download and install all dependencies:
 * MinGW compiler taken through any way:
 * Dynamic version of Qt (with included MinGW compiler)
 * Separated installation of MinGW from official site
 * MinGW-w64 installation
 * 7-zip archivator (or any other which able to unpack 7z archives) to unpack source code

Download sources
To download latest version of source code just use this link (or find download link to latest version on official Qt site) Link to the latest version:
 * http://download.qt.io/official_releases/qt/5.9/5.9.0/single/qt-everywhere-opensource-src-5.9.0.zip

Unpack archive
When you will download archive, open it and unpack into any convenient directory (but note: path must not contain non-ASCII characters [for example, Cyrillic, Chinese or Latin characters with diacritical signs, etc.]).

Create a build script
To build Qt from sources need to make a right build script which will build static Qt.


 * Where QtSrcDir - a full path to directory which contains our unpacked Qt source code (and contains configure.bat file)
 * Where QtStaticDir - a target path where static Qt will be installed (will contain bin, lib, include, plugins, etc. directories).
 * Where MingwDir - a full directory path to MinGW compiler folder (which contains bin, lib, include directories)

Make a "build_static_qt.bat" in any convenient directory and paste contents shown above. Then replace paths in the top paths to fit script into your actual environment, then save it.

Build and install
When you done everything, run your "build_static_qt.bat" script and wait 1.5 ~ 3 hours (dependent to your CPU power) until everything will be built.

Usage
When script will finish it's work, you will have to use the bin/qmake.exe from installation target directory to configure your qt projects to build with static Qt version. You can plug it into Qt Creator with adding reference to bin/qmake.exe from the "Settings" / "Building and debug" tab.