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

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



  • 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]



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



  • I thought Library1 will automatically link Library2.

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



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



  • 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]



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


Log in to reply