Subdirectory in Qt Project

I am looking for a way to organize my project structure using QMake. I found the subdirs pattern, but it's hard for me to understand. I tried to do something like this. Can someone please just tell me that I am right or not.

edit: I read the following thread How do I use a QMake sub-template? but i'm still stuck

My project structure is as follows:

 MultiFuncTester.pro - DMM (DMM.cpp, DMM.h and Multifunctester.pri) -Safety (Safety.cpp, Safety.h and Multifunctester.pri) -Solar (Solar.cpp, Solar.h and Multifunctester.pri) Main (Main.pro, Main.cpp and Multifunctester.pri) 

Here the Multifunctester.pri file has common material for all subdirectories. I am inserting the MultiFuncTester.pro and .pri files as well as the main.pro file

I made a pro file project as MultiFuncTester.pro:

 # build all components recursive TEMPLATE = subdirs ######## normal build process ######## # # Make sure your Main.pro is in the last line to ensure correct linking! # SUBDIRS = ../../MultiFuncTester/Components/Solar/Build/Solar.pro \ ../../MultiFuncTester/Components/DMM/Build/DMM.pro \ ../../MultiFuncTester/Components/Safety/Build/Safety.pro \ ../../MultiFuncTester/Components/Setup/Build/Setup.pro \ ../../MultiFuncTester/Components/Start/Build/Start.pro \ ../../MultiFuncTester/Components/Main/Build/Main.pro \ CONFIG += ordered 

File MultiFunctester.pri:

 ###################### # common stuff for all components ###################### TEMPLATE = lib CONFIG += static \ warn_on \ qt \ thread \ rtti QT += core \ gui INCLUDEPATH +=/..\ ../../MultiFuncTester/Components \ DEPENDPATH +=/..\ ../../MultiFuncTester/Components \ CONFIG += debug_and_release CONFIG += build_all QMAKE_CXXFLAGS += -Wall CONFIG(debug, debug|release) { CONFIG_SUFFIX = dbg } else { CONFIG_SUFFIX = rel DEFINES += QT_NO_DEBUG \ QT_NO_DEBUG_OUTPUT \ DBT_TRACE_DISCARD \ NDEBUG CONFIG(gcov) { QMAKE_CXXFLAGS_RELEASE += -fprofile-arcs -ftest-coverage QMAKE_LFLAGS_RELEASE += -fprofile-arcs QMAKE_CXXFLAGS_RELEASE -= -O2 QMAKE_CXXFLAGS_RELEASE += -O0 } } CONFIG(crosstgt) { ### To be able to build Target run qmake as follows: #qmake CONFIG+=crosstgt CONFIG_SUFFIX = $${CONFIG_SUFFIX}_tgt DEFINES += TARGET_BUILD } OBJECTS_DIR = obj_$${CONFIG_SUFFIX} MOC_DIR = moc_$${CONFIG_SUFFIX} DESTDIR = lib_$${CONFIG_SUFFIX} 

Main.pro file:

 ################# include pri file ################# !include("Main.pri") { error("Main.pri not found") } #################################################### ################# override some pri settings ################# TEMPLATE = app TARGET = MultiFuncTester CONFIG -= static QT += core \ gui ############################################################## ################# list used MultiFuncTester libraries ################# MultiFuncTester_COMPONENTS_DIR =../../MultiFuncTester/Components ################################################################ ################# list MultiFunTester libraries ################# MultiFunTester_COMPONENTS_DIR =../../MultiFuncTester/Components MultiFunTester_COMPONENTS = DMM \ SOLAR\ Safety\ Setup ################# own sources ################# INCLUDEPATH += ../../MultiFuncTester/Components \ SOURCES +=../Source/Main.cpp ################# set destination path DESTDIR = bin_$$CONFIG_SUFFIX ################# edit include path INCLUDEPATH += $$MultiFunctester_COMPONENTS_DIR \ ################# start group LIBS += -Wl,--start-group \ ################# include MultiFunctester libraries and set dependencies for(TheComponent, MultiFunctester_COMPONENTS) { THELIBPATH = $$MultiFunctester_DIR/$${TheComponent}/Build/lib_$$CONFIG_SUFFIX PRE_TARGETDEPS += $$THELIBPATH/lib$${TheComponent}.a LIBS += $$THELIBPATH/lib$${TheComponent}.a } ################# end group LIBS += -Wl,--end-group 

Each subdirectory has a .pro file with headers and sources, as well as a common multifunctester.pri file

Please let me know that hosting a shared static library (MultiFunctester.pri file) is the right approach and what to do in the code ..... and if not, please help me fix me wherever I go wrong.

thanks

+4
source share
1 answer

There is some error:

  • TEMPLATE = lib in MultiFunctester.pri ..pri files are similar to header files, this will be added to every pro file that includes it. This can be misleading, so avoid using the TEMPLATE variable in .pri
  • If I understand your first point well, you have copies of MultiFunctester.pri in each directory. This is also very confusing, you should only have one (or another pri file).
  • Overriding previous settings is not as good as it sounds. When you have to debug information about which configuration is used in each pro file, you will feel pain. Rather, declare all common settings in one place, and declare them for setting variables when they apply.
  • I do not see MultiFunctester.pri anywhere ...

You use the subdirs template if you want each subdirectory to be built separately (they can be interdependent). This is useful, for example, when you have a framework with many executables. look at the qwt source directory

In this case you use

 TEMPLATE = subdirs CONFIG += ordered ### They MUST be subdirectories, no ../, and no .pro SUBDIRS += dir1\ dir2\ ... dirn 

for each level aside from the last (where you have TEMPLATE = lib or app ).

Now, if you have a directory structure, but you want to put everything together, you can create pri for each subdirectory in which you include the source files. For instance:

in the main src.pro file you have

  include(MultiFunctester.pri) ## for the configurations include($PATHTOSOLAR/Solar.pri) include($PATHTODMM/dmm.pri) include($PATHTOSAFETY/safety.pri) include($PATHTOSETUP/setup.pri) include($PATHTOSTART/start.pri) include($PATHTOMAIN/main.pri) 

where $PATHTO... is the relative path from the src.pro directory.

Now in each subdirectory you include the source files.

onesubdir.pri:

  headers += /PATHTOTHISSUBDIR/file1.h ## again same relative path as above .... source +=.... 
0
source

Source: https://habr.com/ru/post/1414843/


All Articles