简体   繁体   English

Qt Creator 和条件构建

[英]Qt Creator and conditional build

In our project, we added some source and header files if a MACRO is defined.在我们的项目中,如果定义了 MACRO,我们添加了一些源文件和头文件。 We do this like that, in the .pro file:我们这样做,在 .pro 文件中:

contains(DEFINES, MY_DEF) {
message("Support MY_DEF")
INCLUDEPATH += \
    my_include_dir
SOURCES += \
    source1.cpp \
    source2.cpp
HEADERS +=  \
    my_include_dir/header1.h \
    my_include_dir/header2.h
FORMS +=  \
    myform.ui
}

This works fine during the build.这在构建过程中工作正常。 The files are not compiled if MY_DEF is not defined.如果未定义 MY_DEF,则不会编译文件。 MY_DEF is defined like that: MY_DEF 定义如下:

DEFINES += MY_DEF

Curiously, Qt Creator always display the files in the project tree, whereas MY_DEF is defined or not.奇怪的是,Qt Creator 总是在项目树中显示文件,而 MY_DEF 是否已定义。 If not defined, they are not used for the build, but they still are displayed and editable, searches can scan them, etc... Is it a bug of Qt Creator?如果没有定义,它们不用于构建,但它们仍然显示和可编辑,搜索可以扫描它们等等......这是Qt Creator的错误吗?

This is not a big issue, just a little annoying, because we don't know clearly if a file is part of the project or not.这不是什么大问题,只是有点烦人,因为我们不清楚一个文件是否是项目的一部分。

It's intentional even.甚至是故意的。 There's a special "accumulating" parsing mode to collect all files that are mentioned in .pro files (essentially the same that's used to collect "translatable strings") for display in the project tree.有一种特殊的“累积”解析模式来收集 .pro 文件中提到的所有文件(本质上与用于收集“可翻译字符串”的文件相同)以显示在项目树中。 Otherwise things like "Replace in all files in a project" would yield different results depending on the platform or the context it is run in. [And it's not half of qmake that's included, but close to all of it...]否则像“替换项目中的所有文件”之类的东西会根据平台或运行它的上下文产生不同的结果。 [而且它不是包含的 qmake 的一半,但接近所有......]

This seems to be an issue with QtCreator and how it reads the .pro files - it doesn't seem to actually fully parse the files, opting instead to just pick out certain bits.这似乎是 QtCreator 以及它如何读取 .pro 文件的一个问题——它似乎并没有真正完全解析文件,而是选择只挑选出某些位。 I've got the same issue with files that are only included on one platform or another - in QtCreator, they always show up.我对只包含在一个或另一个平台上的文件有同样的问题 - 在 QtCreator 中,它们总是出现。

I expect that the reason is either that they don't want to re-implement half of qmake just to get the file lists, OR that there are situations where trying to parse it 'correctly' would get the wrong answer, and they've chosen to be predictably wrong instead of randomly wrong.我预计原因要么是他们不想重新实现一半的 qmake 只是为了获取文件列表,要么在某些情况下尝试“正确”解析它会得到错误的答案,并且他们已经选择是可预见的错误而不是随机错误。

In addition to the conditional includes in QMake, I add #ifdef around such conditional source code.除了 QMake 中的条件包含之外,我还在这些条件源代码周围添加了 #ifdef。 That way I also see it visually drop out of compilation when the conditions are not met.这样我也看到它在不满足条件时从视觉上退出编译。 It's not as good as having the files drop out entirely from the project tree, but it's better than allowing them to still appear like they are part of the build when editing them if they are not applicable.这不如让文件完全从项目树中删除好,但是如果它们不适用,那么在编辑它们时,它仍然比允许它们看起来像它们是构建的一部分要好。

Just for the sake of completeness and answer correctness.只是为了完整性和答案的正确性。 Probably someone else needs this example of root .pro file with conditional source tree:可能其他人需要这个带有条件源树的根 .pro 文件示例:

TEMPLATE = subdirs
SUBDIRS = device

CONFIG -= debug_and_release

_SANDBOX_DIR = $$dirname(PWD)
_PLAYER_PRO = $${_SANDBOX_DIR}/player/player.pro

SUBDIRS = device

device.subdir = $${_SANDBOX_DIR}/proxy/libproxy

contains(QMAKE_PLATFORM, android) {
    unset(_PLAYER_PRO)
} else {
    SUBDIRS += player

    player.file = $${_PLAYER_PRO}
    player.depends = device
}

SUBDIRS += app

app.subdir = $${_SANDBOX_DIR}/display/display
app.depends = device

contains(SUBDIRS, player) {
    app.depends += player
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM