Nominate our 2022 Qt Champions!

QT Projects Organization & Link Static Library

  • Hi All !

    I have a big project and I want to organize it by separate it in to multi projects

    This is the structure of my Projects Tree

    MyApplication (SubDirs)
        Framework (SubDirs)
            DynamicObjectCore (Statically Linked Library)
            Utilities (Statically Linked Library)
        Presentation (App)
        Services (SubDirs)
            GoogleServices (Statically Linked Library)

    I'm use "Add Library" Context Menu and this is generated code in my

    # Link to Framework.DynamicObjectCore
    win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Framework/DynamicObjectCore/release/ -lDynamicObjectCore
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Framework/DynamicObjectCore/debug/ -lDynamicObjectCore
    else:unix: LIBS += -L$$OUT_PWD/../Framework/DynamicObjectCore/ -lDynamicObjectCore
    INCLUDEPATH += $$PWD/../Framework/DynamicObjectCore
    DEPENDPATH += $$PWD/../Framework/DynamicObjectCore
    win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Framework/DynamicObjectCore/release/libDynamicObjectCore.a
    else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Framework/DynamicObjectCore/debug/libDynamicObjectCore.a
    else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Framework/DynamicObjectCore/release/DynamicObjectCore.lib
    else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Framework/DynamicObjectCore/debug/DynamicObjectCore.lib
    else:unix: PRE_TARGETDEPS += $$OUT_PWD/../Framework/DynamicObjectCore/libDynamicObjectCore.a
    # Link to Framework.Utilities
    # Link to Services.GoogleAdWordsService
    # Same as above

    in main.cpp I'm already include need class like this

    // Include from Framework.DynamicObjectCore
    #include "dynamicobject.h"
    #include "MetadataModel/formmetadata.h"
    // Include from Framework.Utilities
    #include "metadatahelper.h"
    int main(int argc, char *argv[])
        //Load Form Metadata
        QString filePath = QCoreApplication::applicationDirPath()+"/Config/FormMetadata/FormMetadata.xml";
        FormMetaData formMetadata = MetadataHelper::GetFormMetaData(filePath);
        DynamicObject* dynamicObject = new DynamicObject();
        QVariant val = dynamicObject->property("Name");

    MetadataHelper::GetFormMetaData is static function

    Why I get this ?
    error: undefined reference to `MetadataHelper::GetFormMetaData(QString)'

  • After few days searching on Google.
    I found the answer.

    The problem is the order you Link to Static Library.

    Dependency of my Projects is:
    Presentation << Utilities << DynamicObjectCore

    in, I'm link to Static Link Library in this order

    # Link to DynamicObjectCore
    # Link to Utilities

    I thought that it will compile DynamicObjectCore first, then compile Utilities.
    But it wan't, and cause the compile error. "undefined reference to..."

    When i change the Link Order

    # Link to Utilities
    # Link to DynamicObjectCore

    And it work.