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
).
CONFIG += static
would solve this. If you don't have static copy of Qt, go grab the source and build. It takes several hours to get static libs. – Md. Minhazul Haque