CMake中option和cmake_dependent_option的使用
CMake中的option命令为用户提供可以选择的布尔选项(boolean option),其格式如下:
option(<variable> "<help_text>" [value])
如果未提供初始<value>,则布尔值OFF是默认值。如果<variable>已设置为普通或缓存变量,则该命令不执行任何操作。
在CMake项目模式(project mode)下,使用选项值创建一个布尔缓存变量。在CMake脚本模式(script mode)下,使用选项值设置一个布尔变量。
option(BUILD_CUDA "build cuda" ON)
if(BUILD_CUDA)message("option BUILD_CUDA: ON") # print
endif()option(BUILD_CAFFE "build caffe" OFF)
if(BUILD_CAFFE)message("option BUILD_CAFFE: ON") # won't print
endif()
if(NOT BUILD_CAFFE)message("option BUILD_CAFFE: OFF") # print
endif()
CMake option在命令行上的设置:形式如: cmake -DBUILD_PYTORCH=ON ..
if(BUILD_PYTORCH)message("option BUILD_PYTORCH: ON") # print
endif()
CMake中的cmake_dependent_option是一个宏,提供依赖于其它选项的选项的宏(macro to provide an option dependent on other options)。仅当一组其它条件为true时,此宏才会向用户提供一个选项。其格式如下:
cmake_dependent_option(<option> "<help_text>" <value> <depends> <force>)
(1).如果<depends>中以分号分割的条件列表全部为true,则使<option>对用户可用。否则,名为<option>的局部变量将设置为<force>.
(2).当<option>可用时,使用给定的<help_text>和初始<value>.否则,用户设置的任何值都会被保留,以备将来满足<depends>时使用。
(3).注意:满足调用者范围内的<depends>条件时<option>变量才只有一个值,因为它是一个局部变量。
option(USE_BAR "USE BAR" ON)
option(USE_ZOT "use zot" OFF)include(CMakeDependentOption) # module CMakeDependentOption provides the cmake_dependent_option macro
cmake_dependent_option(USE_FOO "Use Foo" ON "USE_BAR;NOT USE_ZOT" OFF)if(USE_FOO)message("cmake_dependent_option USE_FOO: ON") # print
endif()
option和cmake_dependent_option中变量的值会存储在CMakeCache.txt中。因此若调整其中变量的值需删除CMakeCache.txt后才会生效。
执行上述测试代码需要3个文件:build.sh, CMakeLists.txt, test_option.cmake
build.sh内容如下:
#! /bin/bash# supported input parameters(cmake commands)
params=(function macro cmake_parse_arguments \find_library find_path find_file find_program find_package \cmake_policy cmake_minimum_required project include \string list set foreach message option if)usage()
{echo "Error: $0 needs to have an input parameter"echo "supported input parameters:"for param in ${params[@]}; doecho " $0 ${param}"doneexit -1
}if [ $# != 1 ]; thenusage
fiflag=0
for param in ${params[@]}; doif [ $1 == ${param} ]; thenflag=1breakfi
doneif [ ${flag} == 0 ]; thenecho "Error: parameter \"$1\" is not supported"usageexit -1
fiif [[ ! -d "build" ]]; thenmkdir buildcd build
elsecd build
fiecho "==== test $1 ===="# test_set.cmake: cmake -DTEST_CMAKE_FEATURE=$1 --log-level=verbose ..
# test_option.cmake: cmake -DTEST_CMAKE_FEATURE=$1 -DBUILD_PYTORCH=ON ..
cmake -DTEST_CMAKE_FEATURE=$1 ..
# It can be executed directly on the terminal, no need to execute build.sh, for example: cmake -P test_set.cmake
CMakeLists.txt内容如下:
cmake_minimum_required(VERSION 3.22)
project(cmake_feature_usage)message("#### current cmake version: ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
include(test_${TEST_CMAKE_FEATURE}.cmake)
message("==== test finish ====")
test_option.cmake:为上面的所有示例代码
可能的执行结果如下图所示:
GitHub:https://github.com/fengbingchun/Linux_Code_Test
CMake中option和cmake_dependent_option的使用相关推荐
- CMake中file的使用
CMake中的file命令用于文件操作,其文件格式如下:此命令专用于需要访问文件系统的文件和路径操作 Readingfile(READ <filename> <variable> ...
- CMake中foreach的使用
CMake中的foreach命令为list中的每个值评估一组命令(Evaluate a group of commands for each value in a list),其格式如下:其中< ...
- CMake中target_compile_features的使用
CMake中的target_compile_features命令用向target添加预期的编译器功能(compiler features),其格式如下: target_compile_features ...
- CMake中include指令用法介绍
转载于: https://blog.csdn.net/liitdar/article/details/81144461 本文主要介绍CMake中include指令的用法. 1 概述 引用CMake官 ...
- CMake中set/unset的使用
CMake中的set命令用于将普通.缓存或环境变量(normal, cache, or environment variable)设置为给定值,其格式如下:指定<value>...占位符( ...
- CMake中link_directories/target_link_directories的使用
CMake中的link_directories命令用于添加目录使链接器能在其查找库(add directories in which the linker will look for librarie ...
- CMake中add_compile_options/target_compile_options的使用
CMake中的add_compile_options命令用于向源文件的编译添加选项,其格式如下: add_compile_options(<option> ...) 将选项添加到COMPI ...
- CMake中target_compile_definitions的使用
CMake中的target_compile_definitions命令用于向target添加编译定义,其格式如下: target_compile_definitions(<target>& ...
- CMake中add_definitions/add_compile_definitions的使用
CMake中的add_definitions命令用于在源文件的编译中添加-D定义标志,其格式如下: add_definitions(-DFOO -DBAR ...) 将当前目录中的target的定义添 ...
最新文章
- vue拖拽控件生成界面代码_Blue HMI人机界面开发平台
- 初学者的困惑:OOP与一般编程的区别
- linux——sed 流编辑器
- 让你的C程序更有效率的10种方法
- covid 19如何重塑美国科技公司的工作文化
- 节后的人才市场开始活跃了?
- Java成神之路[转]
- SPOJ9534 JZPLIT - Turn on the lights(高斯消元)
- python 注销一大段_Django框架用户注销功能实现方法分析
- c语言 指针_C语言野指针以及非法内存操作
- 常见JAVA IO/NIO模型
- 入门Python需要掌握的技能有哪些?
- csdn邻家割草_如何维护割草机,使其永远持续(几乎)
- RCC APB2PeriphClockCmd RCC APB1PeriphClockCmd
- latex范数和|| d ||
- Python 错误:Workbook corruption: seen[2] == 4解决
- (机器学习实战)2.3手写识别系统(详细注释)
- KEPServer软件使用
- chrome浏览器书签
- 如何通过路由器设置上网时间和所访问的网址