Search code examples
c++qtqmakestatic-linking

How to have static linkage in a shared library in Qt Creator?


The question says all the thing. I am using Qt Creator, which uses QMake and I want to build a .so shared library file that has all its dependencies statically linked. Like libstdc++, and etc.

But when I use CONFIG += static it also changes the library to a static library and produces a .a static file, which I don't want it. So my question is not a duplicate of this.

I searched here but I was not able to find any suitable thing.


Solution

  • CONFIG += static is the wrong flag, as stated by the documentation:

    The target is a static library (lib only). The proper compiler flags will automatically be added to the project.

    If you want to link dependencies statically, and produce a shared library, you need to pass a flag to the linker, so add QMAKE_LFLAGS += -static to your .pro file.

    A simple tests results in a 16kb dll without that flag, and a 995kb dll with it. Also, if dependency walker is to be trusted, the larger dll has no external dependencies, while the smaller depend on libgcc and libstdc++ (it is just a trivial std::cout test).

    So evidently, you don't really need a static qt or qmake build. Tested with the "stock" 32bit mingw version of Qt.