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.