Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. linking shared library in static built Qt.

linking shared library in static built Qt.

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samdol
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • S samdol

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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.

        S Offline
        S Offline
        samdol
        wrote on last edited by
        #3

        @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.

        jsulmJ 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          S 1 Reply Last reply
          4
          • S samdol

            @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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              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

              S Offline
              S Offline
              samdol
              wrote on last edited by
              #6

              @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.

              Christian EhrlicherC 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                S Offline
                S Offline
                samdol
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  samdol
                  wrote on last edited by
                  #8

                  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();
                  }

                  1 Reply Last reply
                  0
                  • S samdol

                    @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.

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    @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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    S 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      @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.

                      S Offline
                      S Offline
                      samdol
                      wrote on last edited by
                      #10

                      @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..."

                      jsulmJ 1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @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?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        jsulmJ 1 Reply Last reply
                        2
                        • Christian EhrlicherC Christian Ehrlicher

                          @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?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • S samdol

                            @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..."

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @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.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            S 1 Reply Last reply
                            2
                            • jsulmJ jsulm

                              @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.

                              S Offline
                              S Offline
                              samdol
                              wrote on last edited by
                              #14

                              @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?

                              jsulmJ 1 Reply Last reply
                              0
                              • Christian EhrlicherC Online
                                Christian EhrlicherC Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @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.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                0
                                • S samdol

                                  @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?

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

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

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  2

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved