How to create a layered project structure in C ++ using qmake

In windows using MinGW, C ++ 11, Qt 5 and qmake, I have the following project structure:

/my-project
   my-project.pro
   /my-app
      my-app.pro
      main.cpp
   /module-a
      module-a.pro
      modulea.h
      modulea.cpp
   /module-b
      module-b.pro
      moduleb.h
      moduleb.cpp

The dependencies between the modules should look like this:

my-app ==> module-a ==> module-b

What I want to achieve is that my-app uses module-a, module-a uses module-b, and my-app does not know anything about module-b. The module-module of links-b only through its implementation ( #includefor the module -b is in the .cpp of module-a).

I tried to implement this by setting up modules-a and module-b as static libraries in qmake. Unfortunately, during compilation, I get a linker error saying "undefined reference to ModuleB :: doSmthB ()"

I understand the cause of this binding problem, my question is, is it possible to somehow achieve something similar to the proposed layered structure?

Sources:

my-project.pro:

TEMPLATE = subdirs
SUBDIRS += module-b
SUBDIRS += module-a
SUBDIRS += my-app
my-app.depends = module-a
module-a.depends = module-b

my-app.pro:

QT += widgets
TARGET = my-app
TEMPLATE = app
CONFIG += c++11
SOURCES += *.cpp
win32 {
    INCLUDEPATH += $$clean_path($$PWD/../module-a)
    DEPENDPATH += $$clean_path($$PWD/../module-a)
    LIBS += $$clean_path($$OUT_PWD/../module-a/debug/libmodule-a.a)
    PRE_TARGETDEPS += $$clean_path($$OUT_PWD/../module-a/debug/libmodule-a.a)
}

main.cpp:

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>

#include "modulea.h"

int main(int argc, char *args[])
{
    QApplication app(argc, args);
    QGraphicsView view;
    QGraphicsScene *scene = new QGraphicsScene(0, 0, 300, 300, &view);
    ModuleA moduleA;
    scene->addText(QString::number(moduleA.doSmthA())); // undefined reference to ModuleB::doSmthB()
    view.setScene(scene);
    view.show();
    return app.exec();
}

module-a.pro:

QT -= core gui
TARGET = module-a
TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++11
HEADERS += *.h
SOURCES += *.cpp
win32 {
    INCLUDEPATH += $$clean_path($$PWD/../module-b)
    DEPENDPATH += $$clean_path($$PWD/../module-b)
    LIBS += $$clean_path($$OUT_PWD/../module-b/debug/libmodule-b.a)
    PRE_TARGETDEPS += $$clean_path($$OUT_PWD/../module-b/debug/libmodule-b.a)
}

modulea.h:

#ifndef MODULEA_H
#define MODULEA_H

struct ModuleA
{
    int doSmthA();
};

#endif // MODULEA_H

modulea.cpp:

#include "modulea.h"
#include "moduleb.h"

int ModuleA::doSmthA() {
    ModuleB other;
    return other.doSmthB();
}

module-b.pro:

QT -= core gui
TARGET = module-b
TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++11
HEADERS += *.h
SOURCES += *.cpp

moduleb.h:

#ifndef MODULEB_H
#define MODULEB_H

struct ModuleB
{
    int doSmthB();
};

#endif // MODULEB_H

moduleb.cpp:

#include "moduleb.h"

int ModuleB::doSmthB() {
    return 12345;
}
+4
1

, :

1) CONFIG += create_prl .pro ( module-a). , .

: http://doc.qt.io/qt-5/qmake-advanced-usage.html#library-dependencies

2) .pro( my-project.pro) subdir (SUBDIRS += my-app) ( SUBDIRS += module-a).

, : fooobar.com/questions/92183/...

3) LIBS -L -L ( , ), . :

LIBS += $$clean_path($$OUT_PWD/../module-a/debug/libmodule-a.a)

:

LIBS += -L$$clean_path($$OUT_PWD/../module-a/debug/) -lmodule-a

( ):

my-project.pro:

TEMPLATE = subdirs
SUBDIRS += module-b
SUBDIRS += module-a
SUBDIRS += my-app
my-app.depends = module-a
module-a.depends = module-b

my-app.pro:

QT += widgets
TARGET = my-app
TEMPLATE = app
CONFIG += c++11
SOURCES += *.cpp
win32 {
    INCLUDEPATH += $$clean_path($$PWD/../module-a)
    DEPENDPATH += $$clean_path($$PWD/../module-a)
    LIBS += -L$$clean_path($$OUT_PWD/../module-a/debug/) -lmodule-a
    PRE_TARGETDEPS += $$clean_path($$OUT_PWD/../module-a/debug/libmodule-a.a)
}

module-a.pro:

QT -= core gui
TARGET = module-a
TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++11
CONFIG += create_prl
HEADERS += *.h
SOURCES += *.cpp
win32 {
    INCLUDEPATH += $$clean_path($$PWD/../module-b)
    DEPENDPATH += $$clean_path($$PWD/../module-b)
    LIBS += -L$$clean_path($$OUT_PWD/../module-b/debug/) -lmodule-b
    PRE_TARGETDEPS += $$clean_path($$OUT_PWD/../module-b/debug/libmodule-b.a)
}
+2

All Articles