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.
Introduction
Static version of Qt allows you to build solid application files without packing of a lots of dependent libraries.
WARNING: Don't forget that with the static version of Qt, you can only legally release your GPL v2/v3 and LGPL v3 licensed Open Source applications!
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.
Linux/Mac OS X
Install dependencies
Before start build you must install all necessary dependencies:
Install on Debian/Ubuntu/Mint
sudo apt-get install gcc g++ make wget
sudo apt-get install build-essential
sudo apt-get install \
"^libxcb.*" libx11-dev libx11-xcb-dev libxcursor-dev libxrender-dev libxrandr-dev \
libxext-dev libxi-dev libxss-dev libxt-dev libxv-dev libxxf86vm-dev libxinerama-dev libxkbcommon-dev \
libfontconfig1-dev libharfbuzz-dev \
libasound2-dev libpulse-dev libdbus-1-dev udev mtdev-tools webp \
libudev-dev libglm-dev libwayland-dev libegl1-mesa-dev mesa-common-dev \
libgl1-mesa-dev libglu1-mesa-dev libgles2-mesa libgles2-mesa-dev libmirclient-dev \
libproxy-dev libgtk2.0-dev libgtk-3-dev libcups2-dev
On Ubuntu 14.04 you also will need to install newer GCC compiler and CMake toolchain
sudo add-apt-repository ppa:ubuntu-sdk-team/ppa
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
sudo apt-get install gcc-8 g++-8 cmake
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 80
Install on Mac OS X
(before start you must have pre-installed latest version of XCode)
Via homebrew
brew install coreutils binutils gnu-sed
Via MacPorts
port install bunutils
port install coreutils
port install gsed
Download sources
At first, you must to download entire source code tarball from official Qt site:
https://download.qt.io/archive/qt/5.15/5.15.9/single/qt-everywhere-src-5.15.9.tar.xz
Note: if this link is outdated, you can find newer version of Qt sources here:
Unpack archive
Unpack everything from the tarball into any folder (on Linux or on Mac OS X):
tar -xvf qt-everywhere-opensource-src-5.8.0.tar.gz
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 at the Moondust Project it's useless (main image formats in the Moondust Project were 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:
#!/bin/bash
QTP=qt-everywhere-opensource-src-5.5.1
sed -i 's/qtCompileTest(jasper)/#qtCompileTest(jasper)/g' $QTP'/qtimageformats/qtimageformats.pro'
sed -i 's/jp2 \\/#jp2 \\/g' $QTP'/qtimageformats/src/plugins/imageformats/imageformats.pro'
rm -f $QTP'/qtimageformats/src/3rdparty/jasper.pri'
rm -Rf $QTP'/qtimageformats/src/3rdparty/jasper'
read -n 1
- open command line and execute this script by "bash ./kill_jasper.sh" command
Same for OSX with gnu-sed:
#!/bin/bash
QTP=qt-everywhere-opensource-src-5.5.1
gsed -i 's/qtCompileTest(jasper)/#qtCompileTest(jasper)/g' $QTP'/qtimageformats/qtimageformats.pro'
gsed -i 's/jp2 \\/#jp2 \\/g' $QTP'/qtimageformats/src/plugins/imageformats/imageformats.pro'
rm -f $QTP'/qtimageformats/src/3rdparty/jasper.pri'
rm -Rf $QTP'/qtimageformats/src/3rdparty/jasper'
read -n 1
Configure
Open console then change current directory to new folder which you has been unpacked. then Copy-paste this into console ans press ENTER:
Important note: Qt 5.13.x, 5.14.0, 5.14.1, and 5.14.2 has a bug which will cause all your CMake projects to fail the configure to find the "Qt5::Zlib" module. To fix this annoying bug, you need to modify the "qtbase/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in" file and apply this patch before to configure the Qt.
Linux Mint / Debian (for Qt 5.8 and higher)
./configure -static -release -silent \
-prefix ~/Qt/5.9.0_static \
-opensource -confirm-license -opengl \
-nomake examples \
-skip wayland -skip purchasing -skip serialbus -skip qtserialport -skip script -skip scxml -skip speech \
-system-xcb -qt-libpng -no-libjpeg -qt-zlib -qt-pcre -gtk -system-freetype -system-harfbuzz \
-pulseaudio -alsa
Tip: if you getting failed to configure because of some library (libharfbuzz, libfreetype, etc., try to replace -system-harfbuzz argument with -qt-harfbuzz and also -system-freetype with -qt-freetype.)
macOS Sierra (for Qt 5.8 and higher)
./configure -static -release -silent \
-prefix ~/Qt/5.9.0_static \
-opensource -confirm-license -opengl \
-nomake examples \
-skip wayland -skip purchasing -skip serialbus -skip qtserialport -skip script -skip scxml -skip speech \
-qt-libpng -no-libjpeg -qt-zlib -qt-pcre -qt-harfbuzz -qt-freetype
Tip: To use newer C++ standard than C++11 you add the flag -c++std c++14 or -c++std c++1z.
Linux Mint / Debian (for Qt 5.7 and Qt 5.7.X)
./configure -static -release -nomake examples -nomake tools -prefix ~/Qt/5.7.0_static -system-xcb \
-qt-libpng -no-libjpeg -qt-zlib -qt-pcre -gtk -opensource -confirm-license -opengl \
-qt-freetype -pulseaudio -alsa
Linux Mint / Debian (for Qt 5.6 and lower)
./configure -static -release -nomake examples -nomake tools -prefix ~/Qt/5.6.0_static -system-xcb \
-qt-libpng -no-libjpeg -qt-zlib -qt-pcre -gtkstyle -opensource -confirm-license -gtkstyle -opengl \
-qt-freetype -pulseaudio -alsa -c++11
Mac OS X (for Qt 5.7 and Qt 5.7.X)
./configure -static -release -nomake examples -nomake tools -prefix ~/Qt/5.7.0_static_osx \
-qt-libpng -no-libjpeg -qt-zlib -qt-pcre -gtkstyle -opensource -confirm-license -gtkstyle -opengl -qt-freetype -no-ssse3
then wait while configuring will be finished (you will need to wait ~30...60 min)
Build and install
make
(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)
make -r -j 4
Final step is installation which will copy all built components into target installation directory
make install
(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.
Windows
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:
Important note: Since 5.8 Qt is no more compatible with Windows XP. If you want to keep compatibility, please use Qt 5.7.1 - last version which supports Windows XP:
http://download.qt.io/archive/qt/5.7/5.7.1/single/qt-everywhere-opensource-src-5.7.1.7z
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)
Build script for Qt 5.8 and higher
@echo off
rem ====================CONFIGURE BEFORE RUN SCRIPT!!======================
set QtSrcDir=%CD%\qt-everywhere-opensource-src-5.9.0
set QtStaticDir=C:\Qt\5.9_Static
set MingwDir=C:\Qt\Tools\mingw530_32
set LANG = en
rem =======================================================================
PATH = %MingwDir%\bin;%MingwDir%\opt\bin;%SystemRoot%\system32;%SystemRoot%
set FILE_TO_PATCH=%QtSrcDir%\qtbase\mkspecs\win32-g++\qmake.conf
echo %FILE_TO_PATCH%
if exist %FILE_TO_PATCH%.patched goto skipPatch
type %FILE_TO_PATCH%>%FILE_TO_PATCH%.patched
echo.>>%FILE_TO_PATCH%
echo QMAKE_LFLAGS += -static -static-libgcc>>%FILE_TO_PATCH%
echo QMAKE_CFLAGS_RELEASE -= -O2>>%FILE_TO_PATCH%
echo QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer>>%FILE_TO_PATCH%
echo DEFINES += QT_STATIC_BUILD>>%FILE_TO_PATCH%
:skipPatch
set QT_INSTALL_PREFIX = %QtStaticDir%
set OldDir=%CD%
cd %QtSrcDir%
cmd /C "configure.bat -shared -release -platform win32-g++ -prefix %QtStaticDir% -opensource -confirm-license -c++std c++14 -nomake examples -skip wayland -skip purchasing -skip serialbus -skip qtserialport -skip script -skip scxml -skip speech -skip location -no-opengl -qt-zlib -qt-pcre -qt-libpng -no-libjpeg -qt-freetype -no-openssl"
if ERRORLEVEL 1 goto :error
mingw32-make -r -k -j4
if ERRORLEVEL 1 goto :error
mingw32-make -k install
if ERRORLEVEL 1 goto :error
set FILE_TO_PATCH=%QtStaticDir%\mkspecs\win32-g++\qmake.conf
echo.>>%FILE_TO_PATCH%
echo CONFIG += static>>%FILE_TO_PATCH%
echo ============BUILT!============
goto exitX
:error
echo ============ERROR!============
:exitX
pause
cd %OldDir%
Build script for Qt 5.7
@echo off
rem ====================CONFIGURE BEFORE RUN SCRIPT!!======================
set QtSrcDir=%CD%\qt-everywhere-opensource-src-5.7.0
set QtStaticDir=C:\Qt\5.7_Static
set MingwDir=C:\Qt\Tools\mingw530_32
set LANG = en
rem =======================================================================
PATH = %MingwDir%\bin;%MingwDir%\opt\bin;%SystemRoot%\system32;%SystemRoot%
set FILE_TO_PATCH=%QtSrcDir%\qtbase\mkspecs\win32-g++\qmake.conf
echo %FILE_TO_PATCH%
if exist %FILE_TO_PATCH%.patched goto skipPatch
type %FILE_TO_PATCH%>%FILE_TO_PATCH%.patched
echo.>>%FILE_TO_PATCH%
echo QMAKE_LFLAGS += -static -static-libgcc>>%FILE_TO_PATCH%
echo QMAKE_CFLAGS_RELEASE -= -O2>>%FILE_TO_PATCH%
echo QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer>>%FILE_TO_PATCH%
echo DEFINES += QT_STATIC_BUILD>>%FILE_TO_PATCH%
:skipPatch
set QT_INSTALL_PREFIX = %QtStaticDir%
set OldDir=%CD%
cd %QtSrcDir%
cmd /C "configure.bat -static -release -platform win32-g++ -prefix %QtStaticDir% -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -qt-sql-sqlite -no-openssl -opensource -confirm-license -make libs -nomake tools -nomake examples -nomake tests"
if ERRORLEVEL 1 goto :error
mingw32-make -r -k -j4
if ERRORLEVEL 1 goto :error
mingw32-make -k install
if ERRORLEVEL 1 goto :error
set FILE_TO_PATCH=%QtStaticDir%\mkspecs\win32-g++\qmake.conf
echo.>>%FILE_TO_PATCH%
echo CONFIG += static>>%FILE_TO_PATCH%
echo ============BUILT!============
goto exitX
:error
echo ============ERROR!============
:exitX
pause
cd %OldDir%
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.