qt5 - QMake CONFIG()函数和 'active configuration'

标签 qt5 qmake

在阅读 Qt 5.1(特别是 qmake)的文档时,我对 qmake CONFIG() 函数文档中给出的解释感到困惑。我完全理解该函数的单参数版本,但双参数版本对我来说完全没有意义。我认为我的困惑来自于缺乏“事件配置”的定义,因为 Qt 5.1 文档说了以下内容:

This function can be used to test for variables placed into the CONFIG variable. This is the same as scopes, but has the added advantage that a second parameter can be passed to test for the active config. As the order of values is important in CONFIG variables (that is, the last one set will be considered the active config for mutually exclusive values) a second parameter can be used to specify a set of values to consider.

我非常感谢对“主动配置”这个概念的解释,因为我完全被难住了,无法从第二个论点中得到任何实际意义。

最佳答案

CONFIG 变量可以包含冲突的选项,例如“release”和“debug”。如果 CONFIG 同时包含“release”和“debug”,则“release”或“debug”其中之一有效。 CONFIG 中冲突选项的解释取决于顺序:最后一组将被视为有效或事件配置

使用带有一个参数的 CONFIG() 可以告诉您 CONFIG 变量中是否存在某个选项。如果“release”和“debug”都存在,则 CONFIG(release) 和 CONFIG(debug) 都返回 true。

使用带有两个参数的 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: Running steps for project QMakeConfigTest...

08:53:49: Starting: "C:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qmake.exe" C:\QMakeConfigTest\QMakeConfigTest.pro -r -spec win32-msvc2010

Project MESSAGE: lex yacc debug exceptions depend_includepath testcase_targets import_plugins import_qpa_plugin rtti_off incremental_off windows qt warn_on release link_prl incremental flat 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 Debug build_pass console

08:53:49: The process "C:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qmake.exe" exited normally.

08:53:49: Elapsed time: 00:00.

正如您所看到的,除了 .pro 文件中添加的 console 选项之外,CONFIG 变量还包含许多默认选项。它包含两次 debug 和 release 以及一次 debug_and_release 。

这些默认选项从何而来?它们在 .prf 和 .conf 文件中定义,这些文件是从名为 mkspecs 的目录加载的。因此,您在评论中提出的问题的答案是,在 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。我们来看看qmake手册关于the -spec option :

-spec spec: qmake will use spec as a path to platform and compiler information, and the value of QMAKESPEC will be ignored.

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

关于qt5 - QMake CONFIG()函数和 'active configuration',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164490/

相关文章:

c++ - 如何在 Qt 5 中将 New-Signal-Slot 语法声明为函数的参数

c++ - qobject_cast<QVBoxLayout*>(layout()),是否合适?

android - 如何在 Qt for Android 中使用/链接 OpenCV 3.4.1?

linux - Qmake不包含qt的库路径

android - 如何解决 "make: *** No rule to make target ` out/target/product/generic/root/file_contexts', `snod' 需要。停止。”

c++ - 添加库时出现 Qt 错误 - 没有创建目标的规则

3d - 计算两个网格之间的 Hausdorff 距离

c++ - Gtest 和 Gmock - 双重自由或腐败

c++ - 尝试从资源加载 qml 时出错

c++ - 错误 =>/usr/bin/ld: 找不到 -lQt5::Core -lQt5::Gui -lQt5::Test -lQt5::Concurrent -lQt5::OpenGL