[ACCEPTED]-How to use QMake's subdirs template?-subdirectory

Accepted answer
Score: 87

In addition to Troubadour's comment, I would note that the SUBDIRS target 11 is only good for specifying subdirectories. Therefore, your 10 extra line of

SOURCES += main.cpp

in your project.pro file is 9 incorrect, and will likely fail to build 8 your main.cpp file, at worst. At best, qmake 7 will refuse to parse the file, since it 6 has conflicting specifications in it.

I've 5 used the SUBDIRS template a few times, and it does 4 well if you can build parts into more-or-less 3 independent libraries, apparently like you 2 have with the logic and the gui separate. Here 1 is one way to do this:

----some logic files
----gui files


TEMPLATE = subdirs
SUBDIRS = logic \

# build must be last:
CONFIG += ordered
SUBDIRS += build


#Includes common configuration for all subdirectory .pro files.


# The following keeps the generated files at least somewhat separate 
# from the source files.
UI_DIR = uics
MOC_DIR = mocs


# Check if the config file exists
! include( ../common.pri ) {
    error( "Couldn't find the common.pri file!" )

HEADERS += logic.h
SOURCES += logic.cpp

# By default, TARGET is the same as the directory, so it will make 
# liblogic.a (in linux).  Uncomment to override.
# TARGET = target


! include( ../common.pri ) {
    error( "Couldn't find the common.pri file!" )

FORMS += gui.ui
HEADERS += gui.h
SOURCES += gui.cpp

# By default, TARGET is the same as the directory, so it will make 
# libgui.a (in linux).  Uncomment to override.
# TARGET = target



SOURCES += main.cpp

LIBS += -L../logic -L../gui -llogic -lgui

# Will build the final executable in the main project directory.
TARGET = ../project
Score: 18

You use subdirs if the logic and gui folders actually 15 repesent some sort of target, eg. a library, that 14 can be built independently of anything else. If 13 that's the case then just use

TARGET = logic
CONFIG += dll

in logic.pro.

If 12 they are not independent targets but are 11 just folders that exist to organise the 10 sources files then you can just use a .pri 9 file in each instead and include them within 8 the .pro using


Just remember that the file 7 paths in the .pri files are relative to 6 the .pro file and not the .pri. BTW, the use 5 of a .pri file is optional as you can still 4 list the files in those folders directly 3 in the .pro file. The .pri file just makes 2 it that bit neater and helps keep the .pro 1 file shorter.

More Related questions