Qt static linking and deployment

Viewed 101136

I am trying to deploy(release to public) a simple qt application I made recently, but got stuck at static linking qt libs.

I followed the guide on qt docs to re-build qt and my app statically. But the release build still require qtgui / qtcore dll for no apparent reasons, I wonder if anyone has seen this kind of problems before ? Or even better, has successfully resolved it ?

http://doc.qtsoftware.com/4.5/deployment-windows.html

7 Answers

Also, be aware that your static build will still link to the visual studio runtimes dynamically!

See this faq (internet archive link, in case the link goes away) :

Why does a statically built Qt use the dynamic Visual Studio runtime libraries ? Do I need to deploy those with my application ?

Qt is built using the -MD(d) switch, which links against the dynamic C/C++ runtime libraries. This is necessary as we have experienced memory problems when using anything but the -MD(d) flag, and in general, it is recommended to use. You should not alter this flag yourself for your application, because it conflicts with how the Qt library is built if you change the flag to -MT. You should not change it for Qt either, since it is likely to cause problems.

Qt is still built statically when using the -static option though, meaning you do not need to distribute the Qt dlls when deploying your application. You will have to distribute the C runtimes though (if they don't already exist on the target machine), see our deployment documentation http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies.

msys2 has a pre-built static Qt5 package

e.g. pacman -S mingw-w64-qt5-static

Currently, to also get all dependencies of Qt statically linked, with CMake, you need to add:
list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
before calling find_package(Qt5…


CMAKE_AUTOSTATICPLUGINS was replaced by a new upstream implementation and is not needed anymore.

I recommend you to use linuxdeployqt this tool. It can help solve most of the dependency problems. And I use it to package my qt application successfully.

This answer applies to using MSYS2 with mingw-w64 as the compiler, in Windows, and qmake. The QT version is 5.15.0.


Installing the package qt5-static gives you a build of QT that produces executables who don't rely on any QT DLLs. (Example commandline - pacman -Ss mingw64/mingw-w64-x86_64-qt5-static).

However a new problem is introduced here: it does not pass the -static flag to gcc. Meaning that although the executable does not depend on QT DLLs, it does depend on libgcc-s, libwinpthread.dll etc.

Normally, this problem would be fixed by using CONFIG += static which causes qmake to pass -static to gcc. However, and as noted in the other answers, for a qt-static build that config option is ignored!

To solve this I had to manually specify the gcc flags for static linking in the qmake file, i.e. :

QMAKE_CXXFLAGS += -static
QMAKE_LFLAGS_WINDOWS += -static

Which results in a (large) binary with no external dependencies other than Windows system DLLs.

(In case it matters: my use case was building a COM in-process server DLL that should have no external dependencies; I was also using TEMPLATE=lib and CONFIG += dll).

Related