首页 > 解决方案 > CMake 生成器表达式和 CONFIG 变量查询

问题描述

这个 CMake 文档:https ://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html告诉我们:

$<CONFIG> Configuration name.

然后 :

Conditional generator expressions depend on a boolean condition that must be 0 or 1.

$<condition:true_string> Evaluates to true_string if condition is 1. Otherwise evaluates to the empty string.

$<IF:condition,true_string,false_string> New in version 3.8.

Evaluates to true_string if condition is 1. Otherwise evaluates to false_string.

Typically, the condition is a boolean generator expression. For instance,

$<$<CONFIG:Debug>:DEBUG_MODE> expands to DEBUG_MODE when the Debug configuration is used, and otherwise expands to the empty string.

如果我理解正确,在 Visual Studio 中,如果我从 Debug 切换到 Release 这一行:

$<$<CONFIG:Debug>:DEBUG_MODE>

也应该扩展到 DEBUG_MODE,不是吗?因为根据我的理解,CONFIG 将包含“Release”并且它是一个非空字符串,因此它将扩展为 Debug,而不是由于文档中的以下语句,最后一个生成器表达式将扩展为 DEBUG_MODE:

$condition:true_string 如果条件为 1,则计算为 true_string。否则计算为空字符串。

我知道我错了,请帮助我理解它是如何工作的。

标签: cmake

解决方案


因为 CONFIG 将包含

CONFIG不包含,它不是条件,它是具有特殊规则的特定变量查询。

$<CONFIG:cfgs>

1 if config is any one of the entries in cfgs, else 0. [...]

假设它是这样工作的:

  • 提取零件AB$<A:B>
  • 如果部分ACONFIG - 然后做与CONFIG
  • A例如,如果 part是 - 然后做与 .相关的STREQUAL 事情。BSTREQUAL
  • 对于每种情况...
  • 否则是 a condition,如果是1,则返回B,否则为空字符串。

推荐阅读