How to force libraries to link statically in qt creator

7.5k views Asked by At

When one wants to link statically all available libraries using g++, this person passes -static flag to the compiler.

But how to do it from Qt Creator?

Firstly, I tried this advice: How to make Qt and Qtcreator link the libraries statically instead of dynamic?

The post suggests to use

CONFIG += static

But that just doesn't work.

Secondly, I tried another approach and added the following line to my .pro file:

QMAKE_CXXFLAGS += -static 

That didn't solve the problem either. I analyzed compiler output and discovered:

-static flag is added only when compiling sources to object files. But when building executable, this flag is absent.

Here are the exact compiler commands that I copied from the compiler output tab:

g++ -c -pipe -fno-keep-inline-dllexport -static -g -std=gnu++11 -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -I..\..\..\ASSETS\PROG_CPP\MY_CPP\2016\test -I. -I..\..\..\icu\dist\include -I..\..\5.7\mingw53_32\mkspecs\win32-g++  -o debug\main.o ..\..\..\ASSETS\PROG_CPP\MY_CPP\2016\test\main.cpp
g++ -Wl,-subsystem,console -mthreads -o debug\test.exe debug/main.o  -LC:\icu\dist\lib -lsicuio -lsicuin -lsiculx -lsicule -lsicuuc C:\icu\dist\lib\sicudt.a -lpthread -lm

How to force Qmake to add -static to all commands?

1

There are 1 answers

4
Fedorov7890 On

The solution was quite obvious, as I discovered later. There is a special variable in Qmake that is responsible for linker flags: QMAKE_LFLAGS. So all I had to do is to add the following to the .pro file:

QMAKE_LFLAGS += -static