QMake CONFIG() 函数和“活动配置" [英] QMake CONFIG() function and 'active configuration'

查看:58
本文介绍了QMake CONFIG() 函数和“活动配置"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 Qt 5.1 的文档时,特别是 qmake,我被 qmake CONFIG() 函数文档中给出的解释难住了.我完全理解函数的单参数版本,但双参数版本对我来说完全没有意义.我认为我的困惑来自缺乏对活动配置"的定义,因为 Qt 5.1 文档说明如下:

<块引用>

此函数可用于测试放置在 CONFIG 变量中的变量.这与范围相同,但具有额外的优势,即可以传递第二个参数来测试活动配置.由于值的顺序在 CONFIG 变量中很重要(即,最后一组将被视为互斥值的活动配置)第二个参数可用于指定要考虑的一组值.

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

解决方案

CONFIG 变量可以包含冲突选项,例如发布"和调试".如果 CONFIG 同时包含release"和debug",则release"或debug"都有效.CONFIG 中冲突选项的解释取决于顺序:最后一组将被视为有效或活动配置.

使用带有一个参数的 CONFIG() 会告诉您 CONFIG 变量中是否存在选项.如果同时存在release"和debug",则 CONFIG(release) 和 CONFIG(debug) 都返回 true.

使用带有两个参数的 CONFIG() 告诉你一个选项是否有效,它是 active config 与否.CONFIG(debug, debug|release) 测试debug"是否是debug"和release"选项中的最后一个(因此是活动的).

另见这个问答.

我用 Qt Creator 创建了一个新项目,打开生成的 .pro 文件并在底部添加以下行:message($${CONFIG}) 以便我们可以看到qmake 运行时 CONFIG 的内容.我向您展示整个 .pro 文件:

QT += 核心QT -= gui目标 = QMakeConfigTest配置 += 控制台配置 -= app_bundle模板 = 应用程序源 += main.cpp消息($${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_includepathtestcase_targets import_plugins import_qpa_plugin rtti_offincremental_off windows qt warn_on release link_prl 增量平坦precompile_header autogen_precompile_source debug_and_releasedebug_and_release_target embed_manifest_dll embed_manifest_execopy_dir_files release 共享 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的内容:

# 这个文件在每个实际项目文件之前由 qmake 加载.# 注意从命令行评估变量赋值# 仍然发生在这两个步骤之间.加载(exclusive_builds)配置 = \lex yacc 调试异常depend_includepath \testcase_targets import_plugins import_qpa_plugin \$$配置

如您所见,该文件中定义了前 8 个默认选项.

C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\features\win32\default_pre.prf的内容:

CONFIG = rtti_offincremental_off windows $$CONFIG负载(default_pre)

C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\features\spec_pre.prf的相关部分:

# 这个文件是在加载 qmakespec 之前由 qmake 加载的.# 至此,内置变量已经设置完毕,项目的# .qmake.super 被读取(如果存在).CONFIG = qt warn_on 发布 link_prlQT = 核心 gui

Qt Creator 使用以下选项运行 qmake.exe:-spec win32-msvc2010.让我们看看关于 -spec 的 qmake 手册选项:

<块引用>

-spec spec:qmake 将使用 spec 作为平台和编译器信息的路径,并且 QMAKESPEC 的值将被忽略.

C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\win32-msvc2010\qmake.conf的前几行:

<预><代码>## win32-msvc2010的qmake配置## 为 Microsoft Visual C++ 2010 编写#MAKEFILE_GENERATOR = MSBUILDQMAKE_PLATFORM = win32CONFIG += 增量平面 precompile_header autogen_precompile_source debug_and_release debug_and_release_target embed_manifest_dll embed_manifest_exe定义 += UNICODE WIN32QMAKE_COMPILER_DEFINES += _MSC_VER=1600 WIN32

While reading through the documentation for Qt 5.1, and specifically qmake, I was stumped by the explanation given in the documentation for the qmake CONFIG() function. I completely understood the one-argument version of the function, but the two-argument version makes absolutely no sense to me. I think my confusion is coming from the lack of a definition for 'active config' since the Qt 5.1 documentation says the following:

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.

I would greatly appreciate an explanation for this concept of 'active config' as I am completely stumped and cannot make any practical sense out of this second argument.

解决方案

The CONFIG variable can contain conflicting options, such as both "release" and "debug". If CONFIG contains both "release" and "debug" at the same time then either "release" or "debug" is effective. The interpretation of conflicting options in CONFIG depends on the order: the last one set will be considered the effective or active config.

Using CONFIG() with one parameter tells you whether an option is present or not in CONFIG variable. If both "release" and "debug" are present then both CONFIG(release) and CONFIG(debug) returns true.

Using CONFIG() with two parameters tells you whether an option is effective or not, is it the active config or not. CONFIG(debug, debug|release) tests whether "debug" is the last (and hence, active) among the "debug" and "release" options.

See this question and answer as well.

EDIT:

I've created a new project with Qt Creator, opened the .pro file generated and added the following line at the bottom: message($${CONFIG}) so that we can see the contents of CONFIG when qmake is run. I show you the whole .pro file:

QT       += core
QT       -= gui
TARGET = QMakeConfigTest
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
message($${CONFIG})

There are two lines where CONFIG is modified, only one option is added and one is removed. Then I've chosen Release Build and run qmake. This is what I see in Compile Output window:

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.

As you can see the CONFIG variable contains a lot of default options beside the console option added in the .pro file. It contains both debug and release twice and debug_and_release once.

Where are these default options coming from? They are defined in .prf and .conf files that are loaded from a directory called mkspecs. So the answer to the question you asked in your comment is that before a .pro file is processed by qmake several other files are pre-processed based on your compiler and platform. These files can add the same options more than once and can add conflicting options to the CONFIG variable.

Here is the contents of 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

As you can see the first 8 default options are defined in this file.

The contents of 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)

The relevant part of 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 runs qmake.exe with the following option: -spec win32-msvc2010. Let's see the qmake manual about 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.

The first few lines from 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

这篇关于QMake CONFIG() 函数和“活动配置"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆