如何检测CMake变量是否为缓存变量?
在如何检测CMake变量是否为缓存变量?,cmake,Cmake,在if()中使用什么最简单的方法来检测给定的CMake变量名是否是“缓存变量”而不是常规变量 考虑以下示例: set(regularVariable "some value") set(aVariableInCache "some other value" CACHE INTERNAL "") get_cmake_property(variables VARIABLES) foreach(variable ${variables}) if(???) ... en
if()
中使用什么最简单的方法来检测给定的CMake变量名是否是“缓存变量”而不是常规变量
考虑以下示例:
set(regularVariable "some value")
set(aVariableInCache "some other value" CACHE INTERNAL "")
get_cmake_property(variables VARIABLES)
foreach(variable ${variables})
if(???)
...
endif()
endforeach()
我正在寻找一种简单的方法来区分
regulariable
和aVariableInCache
,只基于哪个在CMake缓存中,哪个只是一个正则变量。我认为,你可以侥幸逃脱
get_property(result CACHE ${variable} PROPERTY TYPE)
result
的空值意味着该变量不在缓存中。自CMake 3.14以来,如果(定义的缓存{var})
可用。
谢谢这就是我昨天实际做的(;这个答案似乎有一个相当严重的问题,它似乎总是将
result
设置为一个错误的值,而不管变量是否为缓存。我在CMake 3.13和CMake 3.16中都测试了这一点。我想你的意思是get_属性(result-cache-variable-property-TYPE)
?如果我省略了${}
aroundvariable
它工作得很好。@jrh我的答案中的代码打算在问题的foreach
循环中使用。在这种情况下,你必须取消引用variable
才能真正获得属性名。嗯……公平点,谢谢。我想再举一个例子给你会很有帮助se也在for循环之外,我可能会键入一些内容。
set(var1 "not cached")
if (DEFINED CACHE{var1})
message("var1 is cache")
else()
message("var1 is not cache")
endif()
set(var2 "cached" CACHE INTERNAL "doc")
if (DEFINED CACHE{var2})
message("var2 is cache")
else()
message("var2 is not cache")
endif()