当前位置:网站首页>cmake / set 命令
cmake / set 命令
2022-06-26 09:36:00 【Ruo_Xiao】
一、介绍
set命令可以设置普通变量、缓存条目、环境变量三种变量的值,对应如下三种命令格式:
set(<variable> <value>... [PARENT_SCOPE]) #设置普通变量
set(<variable> <value>... CACHE <type> <docstring> [FORCE]) #设置缓存条目
set(ENV{<variable>} [<value>]) #设置环境变量设置普通变量时,set的值 <value>...表示可以给变量设置 0 个或者多个值,当设置多个值时(大于2个),多个值会通过分号连接符连接成一个真实的值赋值给变量,当设置 0 个值时,实际上是把变量变为未设置状态,相当于调用 unset命令。
二、使用
1)设置变量为一个给定的值
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (normal_var a)
message (">>> value = ${normal_var}")打印

2)设置变量为多个给定的值
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (normal_var a b c)
message (">>> value = ${normal_var}")打印

3)设置变量为空
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (normal_var a b c)
message (">>> value = ${normal_var}")
set (normal_var) # 设置变量为空
message (">>> value = ${normal_var}")打印

4)如果在函数内,set使用选项PARENT_SCOPE,则变量的作用域只能传递到调用它的函数。
场景1:在函数(示例中为test_fn)内使用set定义变量,带选项PARENT_SCOPE,则在另一个函数(调用者,示例中为test_fn_parent)内调用该函数。
结果:调用者函数内有该变量的定义。
结论:选项PARENT_SCOPE将变量传递到上一层调用函数
# CMakeLists.txt
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
function (test_fn arg1)
set (normal_var_in_fn ${arg1} PARENT_SCOPE)
message (">>> in function, value = ${normal_var_in_fn}")
endfunction (test_fn)
function (test_fn_parent arg1)
test_fn (${arg1})
message (">>> in parent function, value = ${normal_var_in_fn}")
endfunction (test_fn_parent)
test_fn_parent (hello)打印

场景2:在函数内使用选项PARENT_SCOPE定义变量,在函数外使用该变量。
结果:变量为空。
结论:函数内定义的变量,在函数外调用,找不到变量的定义
# CMakeLists.txt
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
function (test_fn arg1)
set (normal_var_in_fn ${arg1} PARENT_SCOPE)
endfunction (test_fn)
test_fn(hello)
message (">>> in directory, value = ${normal_var_fn}")打印

场景3:在函数内直接使用set的变量,带选项PARENT_SCOPE
结果:变量为空。
结论:函数内使用选项PARENT_SCOPE定义的变量,在本函数内也是无定义的
# CMakeLists.txt
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
function (test_fn arg1)
set (normal_var_in_fn ${arg1} PARENT_SCOPE)
message (">>> in function, value = ${normal_var_fn}")
endfunction (test_fn)
test_fn (hello)
message (">>> in directory, value = ${normal_var_fn}")打印

场景4:在函数内,先使用set命令定义该变量,再使用带PARENT_SCOPE选项定义该变量。
结果:函数内的变量值为不带PARENT_SCOPE选项的set命令定义的。
结论:选项PARENT_SCOPE定义的变量作用域在上一层函数,当前函数的变量必须使用不带选项PARENT_SCOPE定义。
# CMakeLists.txt
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
function (test_fn arg1)
set (normal_var_in_fn nohello)
set (normal_var_in_fn ${arg1} PARENT_SCOPE)
message (">>> in function, value = ${normal_var_in_fn}")
endfunction (test_fn)
test_fn (hello)
打印

三、设置缓存条目
命令格式:
set(<variable> <value>... CACHE <type> <docstring> [FORCE])命令含义:
将缓存条目 variable 设置为值 <value>...,除非用户进行设置或使用了选项FORCE,默认情况下缓存条目的值不会被覆盖。缓存条目可以通过 CMAKE 的 GUI 界面的 add entry 按钮来增加。缓存条目的实质为可以跨层级进行传递的变量,类似于全局变量。
缓存条目的<type>主要有以下几类:
BOOL:布尔值ON/OFF,CMAKE 的 GUI 界面对此类缓存条目会提供一个复选框。FILEPATH:文件路径,CMAKE 的 GUI 界面对此类缓存条目会提供一个文件选择框。PATH:目录路径,CMAKE 的 GUI 界面对此类缓存条目会提供一个目录选择框。STRING / STRINGS:文本行,CMAKE 的 GUI 界面对此类缓存条目会提供一个文本框(对应STRING)或下拉选择框(对应STRINGS)。INTERNAL:文本行,但是只用于内部,不对外呈现。主要用于运行过程中存储变量,因此使用该type意味着使用FORCE。
缓存条目的几个注意事项:
- 如果变量先前未定义或者使用了
FORCE选项,则缓存条目会直接被赋值。 - 可以在使用 cmake 构建的使用通过
-D选项来给缓存条目赋值,这样 CMakeLists.txt 内的set命令只会为缓存条目添加类型。 - 如果变量类型是目录或者文件路径,通过
-D选项传入的若只是相对路径,那么set会给这个相对路径前添加当前的工作目录以变成绝对路径(如果已经是绝对路径则不会处理)。
# CMakeLists.txt
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (cache_entry_val ON OFF CACHE BOOL "choose ON to enable")
message (">>> value = ${cache_entry_val}")
set (cache_entry_val2 ON CACHE BOOL "choose ON to enable" FORCE)
message (">>> value2 = ${cache_entry_val2}")
set (cache_entry_val3 ON)
set (cache_entry_val3 OFF CACHE BOOL "choose ON to enable")
message (">>> value3 = ${cache_entry_val3}")
set (cache_entry_input OFF CACHE BOOL "choose ON to enable")
message (">>> value4 = ${cache_entry_input}")
set (mypath "test" CACHE FILEPATH "choose a file path")
message (">>> value5 = ${mypath}")打印

四. 设置环境变量
命令格式:set(ENV{<variable>} [<value>])
命令含义:将环境变量设置为值<value>(注意没有...),接着使用$ENV{<variable>}会得到新的值。cmake中的环境变量可以参考:环境变量。
环境变量设置的几个注意事项:
1)该命令设置的环境变量只在当前的cmake进程生效,既不会影响调用者的环境变量,也不会影响系统环境变量。
2)如果<value>值为空或者ENV{<variable>}后没有参数,则该命令会清除掉当前环境变量的值。
3)<value>后的参数会被忽略。
# CMakeLists.txt
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
message (">>> value = $ENV{CMAKE_PREFIX_PATH}")
set (ENV{CMAKE_PREFIX_PATH} "/test/sub")
message (">>> value = $ENV{CMAKE_PREFIX_PATH}")
set (ENV{CMAKE_PREFIX_PATH})
message (">>> value = $ENV{CMAKE_PREFIX_PATH}")
set (ENV{CMAKE_PREFIX_PATH} "/test/top/")
message (">>> value = $ENV{CMAKE_PREFIX_PATH}")
set (ENV{CMAKE_PREFIX_PATH} "")
message (">>> value = $ENV{CMAKE_PREFIX_PATH}")打印

(SAW:Game Over!)
边栏推荐
- Software testing - how to select the appropriate orthogonal table
- 2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
- This new change of go 1.16 needs to be adapted: the changes of go get and go install
- 动态库连接 - 符号冲突 - 全局符号介入
- LeetCode 基本计算器 224. 227. follow up 394
- Getting started with Flink - word statistics
- Various errors encountered by tensorflow
- Single sign on logic
- install ompl. sh
- thymeleaf中抽取公共片段
猜你喜欢

Redis master-slave replication in win10 system

Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)

2021-11-29 quintic polynomial of trajectory planning

Record a time when the server was taken to mine

Summary of common commands of vim

Druid data source for background monitoring

Force buckle ----- remove the maximum and minimum values from the array

logback

Test instructions - common interface protocol analysis

逻辑英语结构【重点】
随机推荐
Solution to network request crash in retrofit2.8.1
Automated testing -- on the coexistence of Unitest and pytest initialization
SQL高级教程
Custom interceptor
自动化测试——关于unitest与pytest初始化共存问题
力扣------从数组中移除最大值和最小值
Record a time when the server was taken to mine
Logical English structure [key points]
Redis notes (13) - scan and keys search for specific prefix key fields (command format, usage examples, locating large keys)
#云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service
The basis of C language grammar -- function nesting, Fibonacci sum of recursive applet and factorial
libgstreamer-1.0. so. 0: cannot open shared object file: No such file or directory
逻辑英语结构【重点】
How about the security of flush stock trading software? How to open an account in flush
SQL 函数
Vscode common programming fonts
定制拦截器
测试实践——app 测试注意点
Tensorflow dynamically allocates video memory
Druid data source for background monitoring