如何使用QMake的子目录模板?

发布于 2024-08-05 01:41:43 字数 618 浏览 10 评论 0原文

我开始学习Qt。我即将离开 Visual Studio 世界,正在寻找一种使用 QMake 组织项目结构的方法。我找到了“子目录”模板,但我很难理解它。

我的项目结构如下所示:

project_dir/
    main.cpp
    project.pro
    logic/
      logic.pro
      some logic files
    gui/
      gui.pro
      gui files

我的 project.pro 如下所示

TEMPLATE = subdirs
SUBDIRS = logic \
          gui
SOURCES += main.cpp

在子目录的 .pro 文件中,我有适当的 SOURCES >HEADERSRESOURCES 变量设置。

请告诉我应该在 .pro 文件中设置哪些 TARGETTEMPLATE 以及其他必要的值。

另外,除了官方教程之外,还有其他好的 QMake 教程吗?

I'm starting to learn Qt. I'm moving from the Visual Studio world and I am looking for a way to organize my project's structure using QMake. I've found the 'subdirs' template but I have quite a hard time understanding it.

My project structure looks like this:

project_dir/
    main.cpp
    project.pro
    logic/
      logic.pro
      some logic files
    gui/
      gui.pro
      gui files

My project.pro looks like this

TEMPLATE = subdirs
SUBDIRS = logic \
          gui
SOURCES += main.cpp

In the .pro files for the subdirectories I have appropriate SOURCES, HEADERS and RESOURCES variables set.

Please tell me what TARGET, TEMPLATE and other necessary values I should set in the .pro files.

Also, is there some good QMake tutorial other than the official one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤者何惧 2024-08-12 01:41:43

除了 Troubadour 的评论之外,我还想指出 < code>SUBDIRS 目标仅适用于指定子目录。 的额外行

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

project.pro:

TEMPLATE = subdirs
SUBDIRS = logic \
          gui

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

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/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

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

build/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

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

SOURCES += main.cpp

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

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

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

project.pro:

TEMPLATE = subdirs
SUBDIRS = logic \
          gui

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

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/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

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

build/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
憧憬巴黎街头的黎明 2024-08-12 01:41:43

如果逻辑和 gui 文件夹实际上代表某种目标,例如,您可以使用 subdirs一个可以独立于其他任何东西构建的库。如果是这种情况,那么只需

TEMPLATE = lib
TARGET = logic
CONFIG += dll

在logic.pro 中使用即可。

如果它们不是独立的目标,而只是用于组织源文件的文件夹,那么您可以在每个文件中使用 .pri 文件,并将它们包含在 .pro 中,

include(logic/logic.pri)
include(gui/gui.pri)

只需记住 .pri 文件中的文件路径是相对的到 .pro 文件,而不是 .pri。顺便说一句,.pri 文件的使用是可选的,因为您仍然可以直接在 .pro 文件中列出这些文件夹中的文件。 .pri 文件只是让它变得更整洁,并有助于保持 .pro 文件更短。

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

TEMPLATE = lib
TARGET = logic
CONFIG += dll

in logic.pro.

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

include(logic/logic.pri)
include(gui/gui.pri)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文