Using a Static Library in Qt Creator

Using a static library in Qt Creator

LIBS += -L[path to lib] -l[name of lib]

Note! that filename of lib: lib[nameOfLib].a and you have to pass only original part -l[nameOfLib]

How to use static library in Qt 5.2?

You seem to have tried CONFIG+=static, but that is not meant for this use case. That is used when you would like to use build your library to be static after the end of the build.

This is not the case here because you already have static Qt libraries available, so what you wish instead, to link those statically against your executable.

You would need to use this in your qmake project file:

LIBS += -L/path/to/the/static/QtCore -lQtCore

You could also use, albeit this would make the build-system less portable across different platforms:

LIBS += /path/to/the/statis/QtCore/libQtCore.a

windows – Qt create and use self-made static library

You can use QtCreators Subdirs project. Here's a detailed step by step instructions how to achieve that with QtCreator.

  • Pick Subdirs Project from the New Project wizard menu.

Subdirs Project

  • Add Subrojects by clicking on created Subdirs project with right
    mouse button and selecting New Subproject....

New Subproject

  • By following wizards you should have a GUI or console Subproject and
    a library Subproject. Then click on subproject where you want to link
    your library subproject with right mouse button and select Add
    Library...
    .

Add Library

  • Select Internal library in the dialog and you will be prompted to
    choose library you want to add.

Internal library

  • Make sure your library subproject is included before gui/console
    subproject as subdir project will fail to build.

    TEMPLATE = subdirs

    SUBDIRS += \
    LibProject \
    CoreProject

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

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.



Related Topics



Leave a reply



Submit