Qt Creator and conditional build

2019-07-18 09:34发布

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
}

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

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.

标签: qt qt-creator
3条回答
爷、活的狠高调
2楼-- · 2019-07-18 10:04

In addition to the conditional includes in QMake, I add #ifdef around such conditional source code. 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.

查看更多
来,给爷笑一个
3楼-- · 2019-07-18 10:09

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.

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.

查看更多
Lonely孤独者°
4楼-- · 2019-07-18 10:27

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. 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...]

查看更多
登录 后发表回答