CONFIG 变量可能包含相互冲突的选项,例如“release”和“debug”。如果 CONFIG 同时包含“release”和“debug”,那么“release”或“debug”都有效。 CONFIG 中冲突选项的解释取决于顺序:最后一组将被视为有效或active config。
使用带有一个参数的 CONFIG() 可以告诉您 CONFIG 变量中是否存在选项。如果 "release" 和 "debug" 都存在,则 CONFIG(release) 和 CONFIG(debug) 都返回 true。
使用带有两个参数的 CONFIG() 可以告诉您一个选项是否有效,是不是 active config。 CONFIG(debug, debug|release) 测试“debug”是否是“debug”和“release”选项中的最后一个(因此是活动的)。
参见this问答。
编辑:
我用 Qt Creator 创建了一个新项目,打开生成的 .pro 文件并在底部添加以下行:message($${CONFIG}) 以便我们在运行 qmake 时可以看到 CONFIG 的内容。我给你看整个 .pro 文件:
QT += core
QT -= gui
TARGET = QMakeConfigTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
message($${CONFIG})
修改CONFIG的地方有两行,只加了一个选项,去掉了一个。然后我选择了 Release Build 并运行 qmake。这是我在编译输出窗口中看到的:
08:53:49:项目 QMakeConfigTest 的运行步骤...
08:53:49:开始:“C:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qmake.exe”
C:\QMakeConfigTest\QMakeConfigTest.pro -r -spec win32-msvc2010
项目消息:lex yacc 调试异常depend_includepath
testcase_targets import_plugins import_qpa_plugin rtti_off
incremental_off windows qt warn_on release link_prl 增量平面
precompile_header autogen_precompile_source debug_and_release
debug_and_release_target embed_manifest_dll embed_manifest_exe
copy_dir_files release shared rtti qpa win32 msvc debug DebugBuild
调试 build_pass 控制台
08:53:49:进程“C:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qmake.exe”
正常退出。
08:53:49:经过时间:00:00。
正如您所见,CONFIG 变量在 .pro 文件中添加的 console 选项旁边包含许多默认选项。它包含两次 debug 和 release 以及一次 debug_and_release。
这些默认选项从何而来?它们在从名为mkspecs 的目录加载的.prf 和.conf 文件中定义。因此,您在评论中提出的问题的答案是,在 qmake 处理 .pro 文件之前,会根据您的编译器和平台对其他几个文件进行预处理。这些文件可以多次添加相同的选项,并且可以将冲突的选项添加到 CONFIG 变量。
这里是C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\features\default_pre.prf的内容:
# This file is loaded by qmake right before each actual project file.
# Note that evaluating variable assignments from the command line
# still happens in between these two steps.
load(exclusive_builds)
CONFIG = \
lex yacc debug exceptions depend_includepath \
testcase_targets import_plugins import_qpa_plugin \
$$CONFIG
如您所见,该文件中定义了前 8 个默认选项。
C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\features\win32\default_pre.prf的内容:
CONFIG = rtti_off incremental_off windows $$CONFIG
load(default_pre)
C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\features\spec_pre.prf的相关部分:
# This file is loaded by qmake right before loading the qmakespec.
# At this point, the built-in variables have been set up and the project's
# .qmake.super was read (if present).
CONFIG = qt warn_on release link_prl
QT = core gui
Qt Creator 使用以下选项运行 qmake.exe:-spec win32-msvc2010。来看看关于the -spec option的qmake手册:
-spec spec:qmake 将使用 spec 作为平台和编译器信息的路径,
并且 QMAKESPEC 的值将被忽略。
C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\win32-msvc2010\qmake.conf的前几行:
#
# qmake configuration for win32-msvc2010
#
# Written for Microsoft Visual C++ 2010
#
MAKEFILE_GENERATOR = MSBUILD
QMAKE_PLATFORM = win32
CONFIG += incremental flat precompile_header autogen_precompile_source debug_and_release debug_and_release_target embed_manifest_dll embed_manifest_exe
DEFINES += UNICODE WIN32
QMAKE_COMPILER_DEFINES += _MSC_VER=1600 WIN32