如何使用QMake的子目录模板?
我开始学习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、 >HEADERS 和 RESOURCES 变量设置。
请告诉我应该在 .pro 文件中设置哪些 TARGET、TEMPLATE 以及其他必要的值。
另外,除了官方教程之外,还有其他好的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Troubadour 的评论之外,我还想指出 < code>SUBDIRS 目标仅适用于指定子目录。 的额外行
因此, project.pro 文件中 不正确,并且在最坏的情况下可能无法构建 main.cpp 文件。最好的情况是,qmake 会拒绝解析该文件,因为它包含冲突的规范。
我已经使用过几次 SUBDIRS 模板,如果您可以将各个部分构建到或多或少独立的库中,它会做得很好,显然就像您将逻辑和 gui 分开一样。这是一种方法:
project.pro:
common.pri:
logic/logic.pro:
gui/gui.pro:
build/build.pro:
In addition to Troubadour's comment, I would note that the
SUBDIRS
target is only good for specifying subdirectories. Therefore, your extra line ofin 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.pro:
common.pri:
logic/logic.pro:
gui/gui.pro:
build/build.pro:
如果逻辑和 gui 文件夹实际上代表某种目标,例如,您可以使用
subdirs
一个可以独立于其他任何东西构建的库。如果是这种情况,那么只需在logic.pro 中使用即可。
如果它们不是独立的目标,而只是用于组织源文件的文件夹,那么您可以在每个文件中使用 .pri 文件,并将它们包含在 .pro 中,
只需记住 .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 usein 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
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.