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 static library with another static library and then using in a application for QT
Forum Updated to NodeBB v4.3 + New Features

Linking static library with another static library and then using in a application for QT

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 960 Views 1 Watching
  • 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.
  • Maaz MominM Offline
    Maaz MominM Offline
    Maaz Momin
    wrote on last edited by koahnig
    #1

    Hi all,

    I have 2 static library project and a application project.

    In my case, Library1 depends on Library2.

    ---- Library2.pro ----

    QT -= gui
    
    TEMPLATE = lib
    
    CONFIG += debug_and_release
    
    CONFIG(release, debug|release): TARGET = Library2
    else:CONFIG(debug, debug|release): TARGET = Library2d
    
    CONFIG += staticlib
    
    CONFIG += c++11
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    unix:CONFIG(debug, debug|release) {
        DESTDIR = staticlib/debug
    } else {
        DESTDIR = staticlib/release
    }
    
    SOURCES += Library2Class.cpp
    HEADERS += Library2Class.h
    

    ---- Library2Class.h File ----

    class Q_DECL_EXPORT Library2Class
    {
    MyCode...
    }
    

    ---- Library1.pro ----

    QT -= gui
    
    TEMPLATE = lib
    
    CONFIG += debug_and_release
    
    CONFIG(release, debug|release): TARGET = Library1
    else:CONFIG(debug, debug|release): TARGET = Library1d
    
    CONFIG += staticlib
    
    CONFIG += c++11
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    unix:CONFIG(debug, debug|release) {
        DESTDIR = staticlib/debug
    } else {
        DESTDIR = staticlib/release
    }
    
    CONFIG(release, debug|release) {
        LIBS += -L$$PWD/../Library2/staticlib/release/ -lLibrary2
    } else:CONFIG(debug, debug|release) {
        LIBS += -L$$PWD/../Library2/staticlib/debug/ -lLibrary2d
    }
    
    INCLUDEPATH += $$PWD/../Library2
    DEPENDPATH += $$PWD/../Library2
    
    SOURCES += Library1Class.cpp
    HEADERS += Library1Class.h
    

    ---- Library1Class.h File ----

    #include "Library2Class.h"
    
    class Library1Class
    {
    MyCode & using Library2 class
    }
    

    Then I include Library 1 in Application

    --- Application.pro ---

    QT -= gui
    QT += network
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    CONFIG += debug_and_release
    TARGET = Application
    
    TEMPLATE = app
    CONFIG(release, debug|release) {
        LIBS += -L$$PWD/../Library1/staticlib/release/ -lLibrary1
    } else:CONFIG(debug, debug|release) {
        LIBS += -L$$PWD/../Library1/staticlib/debug/ -lLibrary1d
    }
    
    INCLUDEPATH += $$PWD/../Library1
    DEPENDPATH += $$PWD/../Library1
    
    SOURCES += \
            main.cpp \
            console/MyClass.cpp 
    
    HEADERS += \
        console/MyClass.h
    

    ---- MyClass.h File ----

    #include "Library1Class.h"

    class MyClass
    {
    MyCode & using Library1 class
    }
    

    When compiling I am getting following error

    Library1.lib(Library1Class.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Library2Class::Library2Class(class QObject *)" (__imp_??0Library2Class@@QAE@PAVQObject@@@Z) referenced in function "private: void __thiscall library1namespace::calculate::Library1Class::process(class QFileInfo const &)" (?process@Library1Class@calculate@library1namespace@@AAEXABVQFileInfo@@@Z)
    

    [edit:koahnig code tags added]

    K 1 Reply Last reply
    0
    • Maaz MominM Maaz Momin

      Hi all,

      I have 2 static library project and a application project.

      In my case, Library1 depends on Library2.

      ---- Library2.pro ----

      QT -= gui
      
      TEMPLATE = lib
      
      CONFIG += debug_and_release
      
      CONFIG(release, debug|release): TARGET = Library2
      else:CONFIG(debug, debug|release): TARGET = Library2d
      
      CONFIG += staticlib
      
      CONFIG += c++11
      
      DEFINES += QT_DEPRECATED_WARNINGS
      
      unix:CONFIG(debug, debug|release) {
          DESTDIR = staticlib/debug
      } else {
          DESTDIR = staticlib/release
      }
      
      SOURCES += Library2Class.cpp
      HEADERS += Library2Class.h
      

      ---- Library2Class.h File ----

      class Q_DECL_EXPORT Library2Class
      {
      MyCode...
      }
      

      ---- Library1.pro ----

      QT -= gui
      
      TEMPLATE = lib
      
      CONFIG += debug_and_release
      
      CONFIG(release, debug|release): TARGET = Library1
      else:CONFIG(debug, debug|release): TARGET = Library1d
      
      CONFIG += staticlib
      
      CONFIG += c++11
      
      DEFINES += QT_DEPRECATED_WARNINGS
      
      unix:CONFIG(debug, debug|release) {
          DESTDIR = staticlib/debug
      } else {
          DESTDIR = staticlib/release
      }
      
      CONFIG(release, debug|release) {
          LIBS += -L$$PWD/../Library2/staticlib/release/ -lLibrary2
      } else:CONFIG(debug, debug|release) {
          LIBS += -L$$PWD/../Library2/staticlib/debug/ -lLibrary2d
      }
      
      INCLUDEPATH += $$PWD/../Library2
      DEPENDPATH += $$PWD/../Library2
      
      SOURCES += Library1Class.cpp
      HEADERS += Library1Class.h
      

      ---- Library1Class.h File ----

      #include "Library2Class.h"
      
      class Library1Class
      {
      MyCode & using Library2 class
      }
      

      Then I include Library 1 in Application

      --- Application.pro ---

      QT -= gui
      QT += network
      
      CONFIG += c++11 console
      CONFIG -= app_bundle
      
      CONFIG += debug_and_release
      TARGET = Application
      
      TEMPLATE = app
      CONFIG(release, debug|release) {
          LIBS += -L$$PWD/../Library1/staticlib/release/ -lLibrary1
      } else:CONFIG(debug, debug|release) {
          LIBS += -L$$PWD/../Library1/staticlib/debug/ -lLibrary1d
      }
      
      INCLUDEPATH += $$PWD/../Library1
      DEPENDPATH += $$PWD/../Library1
      
      SOURCES += \
              main.cpp \
              console/MyClass.cpp 
      
      HEADERS += \
          console/MyClass.h
      

      ---- MyClass.h File ----

      #include "Library1Class.h"

      class MyClass
      {
      MyCode & using Library1 class
      }
      

      When compiling I am getting following error

      Library1.lib(Library1Class.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Library2Class::Library2Class(class QObject *)" (__imp_??0Library2Class@@QAE@PAVQObject@@@Z) referenced in function "private: void __thiscall library1namespace::calculate::Library1Class::process(class QFileInfo const &)" (?process@Library1Class@calculate@library1namespace@@AAEXABVQFileInfo@@@Z)
      

      [edit:koahnig code tags added]

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Maaz-Momin

      Looks like you did not link against library2 for your application. While creating library1 the routines from library2 are included in the new library. The reference is only for telling where those routines are eventually found.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • Maaz MominM Offline
        Maaz MominM Offline
        Maaz Momin
        wrote on last edited by Maaz Momin
        #3

        I thought Library1 will automatically link Library2.

        Does that mean I need to add Library2 in Application.pro LIBS, INCLUDEPATH & DEPENDPATH ?

        K 1 Reply Last reply
        0
        • Maaz MominM Maaz Momin

          I thought Library1 will automatically link Library2.

          Does that mean I need to add Library2 in Application.pro LIBS, INCLUDEPATH & DEPENDPATH ?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Maaz-Momin

          A reference in LIBS is required for linking. INCLUDEPATH is required when you need to include headers of that library. DEPENDPATH is required when the library might change and you want to avoid problems while building your app. However, a rebuild all for your application would solve the issue with DEPENDPATH.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • Maaz MominM Offline
            Maaz MominM Offline
            Maaz Momin
            wrote on last edited by koahnig
            #5

            By modifying my Application.pro and having following changes everything works fine.

            CONFIG(release, debug|release) {
                LIBS += -L$$PWD/../Library1/staticlib/release/ -lLibrary1
                LIBS += -L$$PWD/../Library2/staticlib/release/ -lLibrary2
            } else:CONFIG(debug, debug|release) {
                LIBS += -L$$PWD/../Library1/staticlib/debug/ -lLibrary1d
                LIBS += -L$$PWD/../Library2/staticlib/debug/ -lLibrary2d
            }
            

            But is there a different solution. As in future if my Library1 depends on another static library OR I want to remove Library2 then again I have to modify my Application.pro

            [edit:koahing code tags added]

            K 1 Reply Last reply
            0
            • Maaz MominM Maaz Momin

              By modifying my Application.pro and having following changes everything works fine.

              CONFIG(release, debug|release) {
                  LIBS += -L$$PWD/../Library1/staticlib/release/ -lLibrary1
                  LIBS += -L$$PWD/../Library2/staticlib/release/ -lLibrary2
              } else:CONFIG(debug, debug|release) {
                  LIBS += -L$$PWD/../Library1/staticlib/debug/ -lLibrary1d
                  LIBS += -L$$PWD/../Library2/staticlib/debug/ -lLibrary2d
              }
              

              But is there a different solution. As in future if my Library1 depends on another static library OR I want to remove Library2 then again I have to modify my Application.pro

              [edit:koahing code tags added]

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              @Maaz-Momin said in Linking static library with another static library and then using in a application for QT:

              But is there a different solution. As in future if my Library1 depends on another static library OR I want to remove Library2 then again I have to modify my Application.pro

              Well, if you have the sources available you could generate one big library. However, what should be the benefit there?

              In general the application.pro is changed frequently anyhow. if you change something to your application which is affecting modules used, you need to change the associated .pro file. AFAIK you should be able to link against libraries which are not used anymore. However, I would not recommend for clarity purposes.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              1

              • Login

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