忽略CMake缓存变量



如何强制CMake忽略缓存值?我想要求调用者每次传递变量,例如

cmake .. -DSOME_VAR=value

如果我调用CMake像

cmake ..

没有SOME_VAR,我希望这段CMake代码失败:

if (NOT DEFINED)
message(FATAL_ERROR " SOME_VAR is missing.")
endif()

似乎unset(SOME_VAR CACHE)不做我所期望的。其实我一点也不关心缓存的变量,我不介意它变慢。

您可以通过在之前测试SOME_VAR来实现这一点project语句,即您的根CMakeLists.txt应该包含这样一段代码:

#very first lines of a CMakeLists.txt
#note: the test must be done **BEFORE** project statement
if(NOT SOME_VAR)
message(FATAL_ERROR "SOME_VAR must be defined in command line")
endif()
project(project_requiring_some_var_in_cmd_line)
#the rest of your CMakeLists.txt 

同样,正如已经指出的,注意这不是使用CMake的自然方式。如果你解释一下你想解决的问题,可能会有帮助。

最新更新