SUBDIRS 示例

qmake 的 SUBDIRS 能力可用于编译一组库,每个库都依赖于另一个库。下面的示例稍微有点复杂,以显示 SUBDIRS 能力的变化。

目录结构

为简洁起见,将省略以下某些文件。可以假设它们是非子实例的格式。

project_dir/
-project.pro
-common.pri
-build.pro
-main.cpp
-logic/
----logic.pro
----some logic files
-gui/
----gui.pro
----gui files

project.pro

这是启用该示例的主文件。这也是在命令行上使用 qmake 调用的文件(见下文)。

TEMPLATE = subdirs  # This changes to the subdirs function.  You can't combine 
                    # compiling code and the subdirs function in the same .pro
                    # file.

# By default, you assign a directory to the SUBDIRS variable, and qmake looks
# inside that directory for a <dirname>.pro file.
SUBDIRS = logic

# You can append as many items as desired.  You can also specify the .pro file
# directly if need be.
SUBDIRS += gui/gui.pro

# You can also create a target that isn't a subdirectory, or that refers to a
# different file(*).
SUBDIRS += build
build.file = build.pro # This specifies the .pro file to use
# You can also use this to specify dependencies.  In this case, we don't want 
# the build target to run until after the logic and gui targets are complete.
build.depends = logic gui/gui.pro

(*)有关子目标的其他选项,请参阅参考文档

common.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.so (in linux).  Uncomment to override.
# TARGET = target

GUI / gui.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.so (in linux).  Uncomment to override.
# TARGET = target

build.pro

TEMPLATE = app

SOURCES += main.cpp

LIBS += -Llogic -Lgui -llogic -lgui

# This renames the resulting executable
TARGET = project

命令行

# Assumes you are in the project_dir directory
> qmake project.pro # specific the .pro file since there are multiple here.
> make -n2 # This makes logic and gui concurrently, then the build Makefile.
> ./project # Run the resulting executable.