zar*_*ych 64 qt qmake subdirectory
我开始学习Qt了.我正在从Visual Studio世界转移,我正在寻找一种使用QMake组织项目结构的方法.我找到了'子目标'模板,但我很难理解它.
我的项目结构如下所示:
project_dir/
main.cpp
project.pro
logic/
logic.pro
some logic files
gui/
gui.pro
gui files
Run Code Online (Sandbox Code Playgroud)
我的project.pro看起来像这样
TEMPLATE = subdirs
SUBDIRS = logic \
gui
SOURCES += main.cpp
Run Code Online (Sandbox Code Playgroud)
在子目录的.pro文件中,我设置了适当的SOURCES,HEADERS和RESOURCES变量.
请告诉我在.pro文件中应该设置的TARGET,TEMPLATE和其他必要值.
另外,还有一些不错的官方QMake教程吗?
Cal*_*itt 85
除了Troubadour的评论之外,我还会注意到SUBDIRS
目标只适用于指定子目录.因此,你的额外线
SOURCES += main.cpp
Run Code Online (Sandbox Code Playgroud)
在你的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
Run Code Online (Sandbox Code Playgroud)
project.pro:
TEMPLATE = subdirs
SUBDIRS = logic \
gui
# build must be last:
CONFIG += ordered
SUBDIRS += build
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
逻辑/ 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
Run Code Online (Sandbox Code Playgroud)
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.a (in linux). Uncomment to override.
# TARGET = target
Run Code Online (Sandbox Code Playgroud)
建立/ build.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
Run Code Online (Sandbox Code Playgroud)
Tro*_*our 17
您可以使用subdirs
如果逻辑和GUI文件夹实际上参阅下文某种目标,例如.一个图书馆,可以独立于其他任何东西建造.如果是这种情况那么只需使用
TEMPLATE = lib
TARGET = logic
CONFIG += dll
Run Code Online (Sandbox Code Playgroud)
在logic.pro中.
如果它们不是独立目标,而只是用于组织源文件的文件夹,那么您可以在每个文件中使用.pri文件,并将它们包含在.pro中使用
include(logic/logic.pri)
include(gui/gui.pri)
Run Code Online (Sandbox Code Playgroud)
请记住.pri文件中的文件路径是相对于.pro文件而不是 .pri.顺便说一句,使用.pri文件是可选的,因为您仍然可以直接在.pro文件中列出这些文件夹中的文件..pri文件只是使它更整洁,有助于缩短.pro文件.