qt - How to create layered project structure in c++ using qmake -
under windows using mingw, c++11, qt 5 , qmake have 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 modules should this:
my-app ==> module-a ==> module-b
what want achieve my-app uses module-a, module-a uses module-b , my-app doesn't know module-b. module-a references module-b through implementation (the #include
module-b located in .cpp of module-a).
i've tried implement configuring module-a , module-b static libraries in qmake. unfortunately while compiling receive linker error saying 'undefined reference moduleb::dosmthb()'
i understand reason of linking problem, question if somehow possible achieve similar 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 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; }
to fix example following changes required:
1) add config += create_prl
.pro files of app's direct dependencies (in case module-a
). won't hurt modify modules this.
explanation here: http://doc.qt.io/qt-5/qmake-advanced-usage.html#library-dependencies
2) in main .pro (in example my-project.pro
) subdir declaration of app (subdirs += my-app
) has placed after direct dependencies of app (after subdirs += module-a
).
a hint second point i've found here: https://stackoverflow.com/a/1417859/6223445
3) libs
property have defined using -l
, -l
options (at least under windows), e.g. instead of:
libs += $$clean_path($$out_pwd/../module-a/debug/libmodule-a.a)
use following:
libs += -l$$clean_path($$out_pwd/../module-a/debug/) -lmodule-a
the fixed solution looks (modified files only):
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) }
Comments
Post a Comment