Cmake Variable
Variable names are case-sensitive and may consist of almost any text.
- 变量的作用域:
Function Scope: 在执行一个函数的时候会创建一个新的作用域, 当前作用域中的所有变量会继承到新的作用域.
Directory Scope: 每个目录也会有自己的变量作用域, 也会继承父目录中的所有变量.
Persistent Scope: 全局作用域, 放缓存变量(Cache Variable).
变量查找时, 会依次查找函数调用栈和当前目录里的变量, 都没有时才会用缓存变量.
set/unset
- set normal variable
set(<variable> <value>... [PARENT_SCOPE])
cache variable
-
缓存变量, 可以在命令行中设置的变量, 缓存在
CMakeCache.txt中 -
在CmakeLists.txt中声明缓存变量:
set(<variable> <value>... CACHE <type> <docstring> [FORCE])
If the cache entry does not exist prior to the call or the
FORCEoption is given
then the cache entry will be set to the given value.
只有加了FORCE才会覆盖已有的缓存变量的值.
- 在命令行中设置缓存变量:
$ cmake ../ D<var>=<value>
environment variable
set(ENV{<variable>} [<value>])
# invocation:
$ENV{foo}
查看帮助信息:
$ cmake --help-variable-list
$ cmake --help-variable PROJECT_BINARY_DIR
浙公网安备 33010602011771号