Qt Creator 和条件构建
在我们的项目中,如果定义了 MACRO,我们添加了一些源文件和头文件.我们这样做,在 .pro 文件中:
In our project, we added some source and header files if a MACRO is defined. We do this like that, in the .pro file:
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
}
这在构建过程中工作正常.如果未定义 MY_DEF,则不会编译文件.MY_DEF 定义如下:
This works fine during the build. The files are not compiled if MY_DEF is not defined. MY_DEF is defined like that:
DEFINES += MY_DEF
奇怪的是,Qt Creator 总是在项目树中显示文件,而 MY_DEF 是否已定义.如果没有定义,它们不用于构建,但它们仍然显示和可编辑,搜索可以扫描它们等等......这是Qt Creator的错误吗?
Curiously, Qt Creator always display the files in the project tree, whereas MY_DEF is defined or not. 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?
这不是什么大问题,只是有点烦人,因为我们不清楚一个文件是否是项目的一部分.
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.
这似乎是 QtCreator 以及它如何读取 .pro 文件的问题 - 它似乎实际上并未完全解析文件,而是选择仅挑选出某些位.对于仅包含在一个或另一个平台上的文件,我遇到了同样的问题 - 在 QtCreator 中,它们总是出现.
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. I've got the same issue with files that are only included on one platform or another - in QtCreator, they always show up.
我希望原因要么是他们不想重新实现一半的 qmake 只是为了获取文件列表,要么在某些情况下尝试正确"解析它会得到错误的答案,并且他们选择了可预见的错误,而不是随机错误.
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.