How to build a version of Qt that can be statically linked

Home / How to build a version of Qt that can be statically linked

You have written your application with Qt and now you are going to deploy it. On MacOSX, you want to use macdeployqt, on Windows you need to copy all needed DLLs next to the executable by hand. And on GNU/Linux? Well, there are two ways: Either you are going to package your Qt app for specific package management systems, there you can make use of the [OpenSUSE build service][1] for example.

The other way is to link as much as possible statically and deploy your app as a huge package within everything ready to run. For this to work, you need to build Qt from scratch statically and link all third party libs in. This tutorial assumes you have your Qt files in “/opt/Qt”. Within this source directory you will find an application called “MaintenanceTool”. Use it to ensure that you have installed “Source Components” for the version of Qt you want to use.

Create a directory next to Src/

mkdir /opt/Qt/5.4/build && cd /opt/Qt/5.4/build

and run the following command. Note that you need to specify the target directory where the finally built static Qt set should be located. I specified “/opt/Qt/5.4-static”, so have them all in one place on my system.

../Src/configure -static -opensource -release -confirm-license -no-compile-examples -nomake tests -prefix /opt/Qt/5.4-static -qt-zlib -qt-libpng -qt-libjpeg -qt-xcb -qt-xkbcommon -qt-freetype -qt-pcre -qt-harfbuzz -gtkstyle -openssl-linked -largefile

Afterwards, run:

make -j 4

Now this may take a long time. When it is finished, run

sudo make install

Now you should be able to select the static Qt set from within QtCreator and rebuild your app with it. If you want to make sure, all went fine, go to the directory the resulting app is in and run

ldd appname

There should be not dependencies to Qt libraries, libpng, libjpeg, libxcb and so on.

Leave a Reply

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