gpt4 book ai didi

cmake检查Mac OS X,使用APPLE还是$ {APPLE}

转载 作者:行者123 更新时间:2023-12-04 02:33:51 26 4
gpt4 key购买 nike

我想检查我是否在Mac OS X中,并具有以下代码

cmake_minimum_required (VERSION 3.0)
project (test)
set (FOO 1)
if (${FOO} AND ${APPLE})
message ("MAC OS X")
endif ()

它在非OSX系统上失败,并显示错误消息
CMake Error at CMakeLists.txt:4 (if):
if given arguments:

"1" "AND"

Unknown arguments specified

如果我将 ${APPLE}替换为 APPLE,则错误消失了。但是我对此有些疑惑。我们什么时候应该用 ${VAR}引用变量,什么时候不应该引用?

提前致谢。

最佳答案

简而言之:if括号内的所有内容均作为表达式求值,这就是if关键字的语义。因此,如果在其中放置APPLE,它将被评估为变量名并产生正确的结果。

现在,如果将${APPLE}放在此处,则${}将在if评估表达式之前先评估其内容。因此,就跟您写的一样

if (1 AND )

(在未设置变量 APPLE的情况下,在非OSX系统上就是这种情况)。这是无效的语法,并产生您得到的错误。您应该写:
if (FOO AND APPLE)

引用 CMake Documentation:

The if command was written very early in CMake’s history, predating the ${} variable evaluation syntax, and for convenience evaluates variables named by its arguments as shown in the above signatures. Note that normal variable evaluation with ${} applies before the if command even receives the arguments. Therefore code like:


set(var1 OFF)
set(var2 "var1")
if(${var2})

appears to the if command as:


if(var1)

and is evaluated according to the if() case documented above. The result is OFF which is false. However, if we remove the ${} from the example then the command sees:


if(var2)

which is true because var2 is defined to “var1” which is not a false constant.

关于cmake检查Mac OS X,使用APPLE还是$ {APPLE},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27660048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com