此外游吟诗人的评论 https://stackoverflow.com/questions/1417776/how-to-use-qmakes-subdirs-template/1417801#1417801,我要指出的是SUBDIRS
target 仅适用于指定子目录。因此,你的额外行
SOURCES += main.cpp
你的project.pro文件中的内容不正确,并且在最坏的情况下可能无法构建你的main.cpp文件。最好的情况是,qmake 会拒绝解析该文件,因为它包含冲突的规范。
我用过SUBDIRS
模板几次,如果您可以将部件构建到或多或少独立的库中,那么它会做得很好,显然就像您将逻辑和 GUI 分开一样。这是执行此操作的一种方法:
project_dir/
-project.pro
-common.pri
-logic/
----logic.pro
----some logic files
-gui/
----gui.pro
----gui files
-build/
----build.pro
----main.cpp
项目.pro:
TEMPLATE = subdirs
SUBDIRS = logic \
gui
# build must be last:
CONFIG += ordered
SUBDIRS += build
通用.pri:
#Includes common configuration for all subdirectory .pro files.
INCLUDEPATH += . ..
WARNINGS += -Wall
TEMPLATE = lib
# The following keeps the generated files at least somewhat separate
# from the source files.
UI_DIR = uics
MOC_DIR = mocs
OBJECTS_DIR = objs
逻辑/logic.pro:
# 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
图形用户界面/图形用户界面.pro:
! 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
构建/构建.pro:
TEMPLATE = app
SOURCES += main.cpp
LIBS += -L../logic -L../gui -llogic -lgui
# Will build the final executable in the main project directory.
TARGET = ../project