Build Static Qt 5.13.0 for Linux (29/7/19)
While working on Qamel, I want it to support compiling static app for Linux and Windows. When targetting Windows, this is simple enough thanks to project like MXE and MSYS2 which already provided static Qt libraries. However, when targeting Linux platform, there are no choice except building static Qt libraries by ourself.
In this tutorial, I will use Ubuntu 16.04 and the static Qt will be build into
sudo apt-get update sudo apt-get install build-essential libgl1-mesa-dev \ libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev \ libxfixes-dev libxi-dev libxrender-dev libxcb1-dev \ libx11-xcb-dev libxcb-glx0-dev libxkbcommon-dev \ libxkbcommon-x11-dev '^libxcb.*-dev'
Next, open terminal inside the directory where you extract the source code. Here we will run
./configure to specify that we want to build static libraries. There are many parameters that can be changed which can be seen in documentation or by running
./configure -h. In my case, I do it like this :
./configure -static -prefix "/opt/Qt$QT_VERSION" \ -opensource -confirm-license -release \ -optimize-size -strip -fontconfig \ -qt-zlib -qt-libjpeg -qt-libpng -qt-xcb \ -qt-pcre -qt-harfbuzz -qt-doubleconversion \ -nomake tools -nomake examples -nomake tests \ -no-pch -skip qtwebengine
Here's description for the parameters that I used :
staticspecify that we want to build static version;
prefixis where to put the result;
open-sourcemeans we will use the open source license, in this case LGPLv3;
confirm-licensetells that we accept the license, therefore we won't be asked again later;
releasetells that the output Qt libraries will not support debugging. This is used because in my case the static build will only used for publishing, therefore there is no need to support debugging there;
optimize-sizetells that when building our app, we want its size optimized even if it increase the build time for our app;
stripwill strips the released binaries of unneeded symbols;
fontconfigtells that our Qt libraries will use fontconfig that provided by system instead of the one that provided by Qt. This is used to avoid ugly error warning about missing fontconfig in some distro;
qt-*tells that our Qt libraries will use the libraries that provided by Qt instead of the one that provided by system;
nomake toolsis used because we don't need to build Qt tools (like Qt Creator);
nomake examplesbecause we don't need to build examples;
nomake testsbecause we don't need to build tests;
no-pchmeans no precompiled headers, meaning that all the header files (.h) will not be precompiled so we'll get all the benefits from
#ifdef. This one is used following tutorial from Retifrav;
skip qtwebenginebecause I don't use it.
Once the configuration process finished, we only need to build it :
make -j $(grep -c ^processor /proc/cpuinfo)
This might take a long time depending on number of CPU core that available to use. In my case, it took almost 3-4 hours until it finished. Once the build finished, we only need to install it to our specified
prefix by running :
Once finished, the static Qt libraries is already built and ready to use.