当前位置:网站首页>Cmake / set command
Cmake / set command
2022-06-26 10:09:00 【Ruo_ Xiao】
One 、 Introduce
set Command can set common variables 、 cache line 、 Environment variables are the values of three variables , Corresponding to the following three command formats :
set(<variable> <value>... [PARENT_SCOPE]) # Set common variables
set(<variable> <value>... CACHE <type> <docstring> [FORCE]) # Set cache entries
set(ENV{<variable>} [<value>]) # Set the environment variable When setting normal variables ,set Value <value>... Indicates that the variable can be set 0 One or more values , When setting multiple values ( Greater than 2 individual ), Multiple values will pass Semicolon connector Connect to a real value and assign it to the variable , When setting 0 When it's worth , It's actually changing the variable to an unset state , Equivalent to calling unset command .
Two 、 Use
1) Set the variable to a given value
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (normal_var a)
message (">>> value = ${normal_var}")
2) Set the variable to multiple given values
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (normal_var a b c)
message (">>> value = ${normal_var}")
3) Set the variable to null
cmake_minimum_required (VERSION 3.10.2)
project (set_test)
set (normal_var a b c)
message (">>> value = ${normal_var}")
set (normal_var) # Set the variable to null
message (">>> value = ${normal_var}")
4) If in a function ,set Use options PARENT_SCOPE, Then the scope of the variable can only be passed to the function calling it .
scene 1: In function ( The example is test_fn) Internal use set Defining variables , With options PARENT_SCOPE, Then in another function ( caller , The example is test_fn_parent) Call this function inside .
result : The variable is defined in the caller function .
Conclusion : Options PARENT_SCOPE Pass the variable to the upper layer and call the function
# 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)
scene 2: Use options inside functions PARENT_SCOPE Defining variables , Use this variable outside the function .
result : The variable is empty .
Conclusion : Variables defined in functions , Call... Outside the function , Cannot find definition of variable
# 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}")
scene 3: Use... Directly within a function set The variable of , With options PARENT_SCOPE
result : The variable is empty .
Conclusion : Use options inside functions PARENT_SCOPE Variables defined , It is also undefined in this function
# 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}")
scene 4: Within the function , First use set Command defines the variable , Reuse the tape PARENT_SCOPE Option defines the variable .
result : The value of the variable in the function is No PARENT_SCOPE Option set Command defined .
Conclusion : Options PARENT_SCOPE The defined variable scope is in the upper function , The variables of the current function must be used without options PARENT_SCOPE Definition .
# 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)

3、 ... and 、 Set cache entries
Command format :
set(<variable> <value>... CACHE <type> <docstring> [FORCE]) Meaning of order :
Cache entries variable Set to value <value>..., Unless the user makes a setting or uses an option FORCE, By default, the value of the cache entry is not overwritten . Cache entries can be accessed through CMAKE Of GUI Interface add entry Button to add . The essence of cache entries is variables that can be passed across levels , Similar to global variables .
Cache entry <type> There are mainly the following categories :
BOOL: Boolean valueON/OFF,CMAKE Of GUI The interface will provide a check box for such cache entries .FILEPATH: File path ,CMAKE Of GUI The interface will provide a file selection box for such cache entries .PATH: Directory path ,CMAKE Of GUI The interface will provide a directory selection box for such cache entries .STRING / STRINGS: Text row ,CMAKE Of GUI The interface will provide a text box for such cache entries ( CorrespondingSTRING) Or drop-down selection box ( CorrespondingSTRINGS).INTERNAL: Text row , But only for internal , No external presentation . It is mainly used to store variables during operation , So use thistypeIt means usingFORCE.
Several considerations for caching entries :
- If the variable is not previously defined or used
FORCEOptions , The cache entry will be assigned directly . - Can be used in cmake The use of the build is through
-DOption to assign a value to the cache entry , such CMakeLists.txt InternalsetThe command only adds types to cache entries . - If the variable type is directory or file path , adopt
-DIf only the relative path is passed in by the option , thatsetThe current working directory will be added before the relative path to become an absolute path ( If it is already an absolute path, it will not process ).
# 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}")
Four . Set the environment variable
Command format :set(ENV{<variable>} [<value>])
Meaning of order : Set the environment variable to the value <value>( Note that there is no ...), Then use $ENV{<variable>} You'll get a new value .cmake The environment variables in can refer to : environment variable .
Several considerations for setting environment variables :
1) The environment variable set by this command is only in the current cmake Entry into force , It will not affect the caller's environment variables , It also does not affect the system environment variables .
2) If <value> The value is empty or ENV{<variable>} There are no parameters after , The command will clear the value of the current environment variable .
3)<value> The parameters after are ignored .
# 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}")
Reprint :cmake Order set_ A blog where everything turns as it pleases -CSDN Blog _cmake set command
(SAW:Game Over!)
边栏推荐
- Day 3 array, pre post, character space, keyword and address pointer
- c语言语法基础之——指针(字符、一维数组) 学习
- c语言语法基础之——函数 小程序 求阶乘
- Openxcap usage
- Cloud native essay using Hana expression database service on Google kubernetes cluster
- Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
- Vscode common programming fonts
- Learning and understanding of thread pool (with code examples)
- Meaning of go runtime
- How to find and install the dependent libraries of Debian system
猜你喜欢

Cloud native essay using Hana expression database service on Google kubernetes cluster

The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions

The first batch of 12 enterprises settled in! Opening of the first time-honored product counter in Guangzhou

字符串常量池、class常量池和运行时常量池

DAY 3 数组,前置后置,字符空间,关键词和地址指针

mysql学习总结

Appium automation test foundation - mobile end test environment construction (II)

WGCLOUD的web ssh服务端口是多少

online trajectory generation

首批12家企业入驻!广州首个集中展销老字号产品专柜开张
随机推荐
测试实践——app 测试注意点
Jupyter Notebook遇到的问题
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
Redis notes (13) - scan and keys search for specific prefix key fields (command format, usage examples, locating large keys)
[trajectory planning] testing of ruckig Library
定制拦截器
【Leetcode】76. 最小覆盖子串
Basic grammar of C language -- pointer (character, one-dimensional array) learning
国际化配置
Force buckle ----- remove the maximum and minimum values from the array
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
美国总统签署社区安全法案以应对枪支问题
【无标题】
Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
Detailed explanation of winsorflow quantum installation process
cento7.7安装ELK简单记录
Dialog centered
测试须知——常见接口协议解析
jar版本冲突问题解决
Redis novice introduction