Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

linking shared library in static built Qt.



  • Hi,

    I did static build Qt 64bit. And I found a third party shared library on internet.
    I tried to use in my project. but I could not link to the shared library.
    It is possible to link shared library to my application producted by statically built Qt?
    If not, is there any work around to link?
    Thank you.


  • Lifetime Qt Champion

    @samdol said in linking shared library in static built Qt.:

    I tried to use in my project

    Please show how.
    And also tell us what exactly did not work.



  • @jsulm When I created a shared library using Qt-creator with Qt installed by qt-opensource-windows-x86-mingw492-5.6.2.exe, I could get mylib.a and mylib.dll. but when I created a shared library using Qt-creator with static-built Qt, I could get only mylib.a without mylib.dll.
    I don't think I can link the library without .dll. I don't know what did I make mistake.


  • Lifetime Qt Champion

    When I created a shared library using Qt-creator with Qt installed by qt-opensource-windows-x86-mingw492-5.6.2.exe, I could get mylib.a and mylib.dll. but when I created a shared library using Qt-creator with static-built Qt, I could get only mylib.a without mylib.dll.

    You're wrong here.
    When you compile static then you will only get a .a
    When you create a shared lib you will get a .dll (the shared lib) and an import lib (.a).

    I don't think I can link the library without .dll

    You don't link against the dll, you link against the static (import) lib .a


  • Lifetime Qt Champion

    @samdol Do you have to link against shared version? If not simply use the static one.



  • @Christian-Ehrlicher
    I selected shared(not static) library when I create the library using qt-creator. It seems I get only .a because my Qt is built statically.



  • @jsulm This is for my practice. My real project using static Qt links statically to MIT third party libraries. but it also has to link dynamically to LGPL third party libraries.



  • I could get both .a and .dll by adding
    CONFIG += shared
    to .pro
    I put the header files to myapp/include directory and
    generated .a and .dll to myapp/lib.
    So I could link the library to myapp, but when I ran myapp,
    it did not pop up widget and gives the following message:

    Starting D:\my\build-myapp-64bit-Release\release\lib_app.exe...
    QWidget: Must construct a QApplication before a QWidget
    D:\my\build-myapp-64bit-Release\release\lib_app.exe exited with code 3

    Another thing strange is that the generated libtestLib.a is 9kb but
    the generated testLib.dll is HUGE 12,892kb even I have only one widget
    derived from QWidget.

    testLib.pro
    QT += widgets
    TARGET = testLib
    TEMPLATE = lib
    DEFINES += TESTLIB_LIBRARY
    SOURCES += widget.cpp
    HEADERS += widget.h
    testlib_global.h
    CONFIG += shared

    testlib_global.h
    #ifndef TESTLIB_GLOBAL_H
    #define TESTLIB_GLOBAL_H
    #include <QtCore/qglobal.h>
    #if defined(TESTLIB_LIBRARY)

    define TESTLIBSHARED_EXPORT Q_DECL_EXPORT

    #else

    define TESTLIBSHARED_EXPORT Q_DECL_IMPORT

    #endif
    #endif // TESTLIB_GLOBAL_H

    widget.h
    #ifndef WIDGET_H
    #define WIDGET_H
    #include <QWidget>
    #include <QDebug>
    #include "testlib_global.h"
    class TESTLIBSHARED_EXPORT Widget : public QWidget
    {
    Q_OBJECT
    public:
    Widget();
    void print();
    };
    #endif // WIDGET_H

    widget.cpp
    #include "widget.h"
    Widget::Widget() : QWidget()
    {
    qDebug()<<"My library runs successfully.";
    }
    void Widget::print()
    {
    qDebug()<<"print() is called.";
    }

    myapp.pro
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TEMPLATE = app
    TARGET = lib_app
    INCLUDEPATH += $$PWD/include
    LIBS += -L$$PWD/lib -ltestlib
    SOURCES += main.cpp

    main.cpp
    #include <QApplication>
    #include <QWidget>
    #include "widget.h"
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    w.resize(100, 100);
    w.show();
    return a.exec();
    }


  • Lifetime Qt Champion

    @samdol said in linking shared library in static built Qt.:

    because my Qt is built statically.

    Then you can't build a shared lib by default, yes this is correct.

    Another thing strange is that the generated libtestLib.a is 9kb but
    the generated testLib.dll is HUGE 12,892kb even I have only one widget
    derived from QWidget.

    And this is correct too - the static (import) lib only contains your stuff, the shared lib needs the whole Qt code.



  • @Christian-Ehrlicher
    Thank you for the reply. When I build testLib.dll using Qt-creator with Qt installed by qt-opensource-windows-x86-mingw492-5.6.2.exe, it was only few hundreds kilobytes. But with static built Qt it is 12 MB which I don't understand.

    On the other hand, in win10 64bit v1803, after linking to the shared library, it did not pop up widget with perplexing message:
    QWidget: Must construct a QApplication before a QWidge
    I tried to run it on the other machine which has win 10 64bit v1709, ant it pops up the message "Runtime error..."


  • Lifetime Qt Champion

    @samdol said in linking shared library in static built Qt.:

    static built Qt it is 12 MB which I don't understand.

    Again: You shared lib contains all Qt code it needs from your static Qt.

    Why do you want to use a static Qt at all?


  • Lifetime Qt Champion

    @Christian-Ehrlicher Small correction: the static lib contains whole Qt :-)


  • Lifetime Qt Champion

    @samdol said in linking shared library in static built Qt.:

    But with static built Qt it is 12 MB which I don't understand

    Then you should read about static libs first before using them. When using static libs all the stuff from that lib is linked into your app/lib and that's why it is bigger then. This is a fundamental difference between static and shared lib.



  • @jsulm said in linking shared library in static built Qt.:

    This is a fundamental difference between static

    Can I mix to use static and shared library for the same app?
    About runtime error:
    I think it maybe mingw version issue: I saw the third party shared library is compiled with Qt5.7 mingw5.3.0. But My app is compiled with Qt5.6.2 mingw4.9.2. If the compiler version should be the same, do I have to whole build Qt5.6.2 with mingw5.3.0 or can I just set mingw5.3.0 in build option in Qt-Creator with Qt built with mingw4.9.2?


  • Lifetime Qt Champion

    @samdol said in linking shared library in static built Qt.:

    Can I mix to use static and shared library for the same app?

    No, not really - and this questions shows us - just stop with the static stuff, stay with the shared libraries.


  • Lifetime Qt Champion

    @samdol You need Qt built with same compiler - do not mix binaries built with different C++ compilers.


Log in to reply