4

I am looking for a way to organize my project's structure using QMake. I've found the subdirs template but I have quite a hard time understanding it. I have tried to do something like this. Can anyone please just tell me that I am correct or not.

edit: I have read the following thread How to use QMake's subdirs template? but I am 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 Multifunctester.pri file has common stuff for all the sub directories. I am pasting the MultiFuncTester.pro and .pri file and also the main.pro file

I have made project's pro file 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

MultiFunctester.pri file:

######################
# 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 which have headers and sources defined and also the common multifunctester.pri file

Please let me know that putting a common static library (MultiFunctester.pri file) is a right approach and what shall it do in the code.....and if not please help me in correcting me wherever I am wrong.

Thanks

Community
  • 1
  • 1
pragati
  • 95
  • 2
  • 9
  • 1
    do you have pro files in each subdirectory? if it is the case why?? – UmNyobe May 29 '12 at 09:06
  • yes in each subdirectory I have their own pro file which states what shall be the target, what are the sources, includes, forms and resources. – pragati May 29 '12 at 09:10

1 Answers1

1

There are some mistake :

  • TEMPLATE = lib in MultiFunctester.pri. .pri files are like header files, this will be added in every pro files which include it. It can be confusing, so avoid using the TEMPLATE variable in .pri
  • if I understand well your first point you have copies of Multifunctester.pri in each directory. This is also really confusing, you should only have one (or different pri files).
  • Overriding previous setting is not as a good idea as it seems. When you will have to debug to see which configuration is used in each pro file you will feel the pain. Rather declare all common settings in one place, and for variable settings declare them when they apply.
  • I don't see Multifunctester.pri included anywhere...

You use the subdirs template when you want each sub-directory to be built separately (they can be interdependent). It is useful for instance when you have a framework with many executable. look at 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 of the last one (where you have TEMPLATE = lib or app).

Now if you have directory structure but you want to build all together you can create a pri file for each subdirectory where you include the source files. For example :

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 thesrc.pro directory.

Now in each subdirectory you include the source files.

onesubdir.pri :

 headers += /PATHTOTHISSUBDIR/file1.h ## again same relative path as above
       ....
 source +=....
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Have you gone through the post which is linked in this question? – pragati May 29 '12 at 09:43
  • Actually This code I have posted is running fine. A simple hello world is easily displayed. But when I try to use any function from one of the directory say start...I get error that undefined refernce to Start....I main.o and start.o are not getting linked properly...I dont know why? – pragati May 29 '12 at 09:48
  • then it is not running fine.... look at qwt. that's the standard way of proceeding.. – UmNyobe May 29 '12 at 09:50
  • If I don't use .pro while defining directories in subdirs how would I get different different folders for each subdirectories – pragati May 29 '12 at 11:17
  • I tried your steps too on windows platform on qt creator, still getting same errors – pragati May 29 '12 at 12:24
  • 1. onesubdir.pri : example of a PRI file of one sub- directory. 2. Windows or linux, no difference. 3. SUBDIRS take subdirectory as arguments, simple as that. Not pro. It look automatically for the corresponding pro file. 4. I provided 2 alternatives: Which steps did you follow 5. Which errors?? be specific. – UmNyobe May 29 '12 at 13:51
  • In main I get errors as undefined reference to the function and classes defined in the subdirectories – pragati May 30 '12 at 04:41
  • Let me tell you my requirement may be then you can suggest me a good answer. I have to make GUI for an embedded device. Now in this device there are four different functionality. Some of the screens can be same in all these four functionality. Now I am trying to divide my project into four subdirectories which represent these four functionalties. Also I want that there shall be one main and one executable generated and all these subdirectories can be linked to each other. With the above defined code I am able to create a directory structure but my main is not able to find the sub directories. – pragati May 30 '12 at 04:49
  • I don't know I didnt got any success with your approach but the one described in the link i mentioned in my question...that finally worked...guess I have to look into makefile....but thanks anyways I would try to get this to be done in your way too.... – pragati Jun 04 '12 at 06:42