当前位置:网站首页>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!)
边栏推荐
- Leetcode refers to offer II 091 Paint house - modify in place
- Standard implementation of streaming layout: a guide to flexboxlayout
- SQL function
- Extracting public fragments from thymeleaf
- Various errors encountered by tensorflow
- Svn command
- WIN10系统实现Redis主从复制
- Software testing - how to select the appropriate orthogonal table
- 力扣------从数组中移除最大值和最小值
- Automated testing -- Introduction and use of pytest itself and third-party modules
猜你喜欢
Single sign on logic
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
Differences between JVM, Dalvik and art
软件测试---如何选择合适的正交表
Install new version cmake & swig & tinyspline
[trajectory planning] testing of ruckig Library
Redis novice introduction
The 100000 line transaction lock has opened your eyes.
Record a time when the server was taken to mine
DAY 3 数组,前置后置,字符空间,关键词和地址指针
随机推荐
Differences between JVM, Dalvik and art
Redis notes (16) - info instructions and command line tools (view memory, status, number of client connections, monitoring server, scan large keys, sampling server, execute batch commands, etc.)
Battery historian analyzes battery consumption
如何更改微信小程序二维码物料颜色
In the fragment, the input method is hidden after clicking the confirm cancel button in the alertdialog (this is valid after looking for it on the Internet for a long time)
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
Jupyter Notebook遇到的问题
动态库连接 - 符号冲突 - 全局符号介入
My creation anniversary
Teach you to use shell script to check whether the server program is running
Abnormal record-23
DAY 3 数组,前置后置,字符空间,关键词和地址指针
Halcon photometric stereoscopic
JSP file syntax
Use recursion or a while loop to get the name of the parent / child hierarchy
Software testing - how to select the appropriate orthogonal table
Develop current learning objectives and methods
TensorFlow遇到的各种错误
Testing practice - App testing considerations