CMake中option和cmake_dependent_option的使用

本文详细介绍了CMake中的option和cmake_dependent_option命令,包括它们的语法、用法示例以及在命令行上的设置。通过实例演示了如何创建可选择的布尔变量,并探讨了cmake_dependent_option如何根据依赖条件提供选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      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[@]}; do
		echo "  $0 ${param}"
	done

	exit -1
}

if [ $# != 1 ]; then
	usage
fi

flag=0
for param in ${params[@]}; do
	if [ $1 == ${param} ]; then
		flag=1
		break
	fi
done

if [ ${flag} == 0 ]; then
	echo "Error: parameter \"$1\" is not supported"
	usage
	exit -1
fi

if [[ ! -d "build" ]]; then
	mkdir build
	cd build
else
	cd build
fi

echo "==== 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:为上面的所有示例代码

      可能的执行结果如下图所示:

       GitHubhttps://github.com/fengbingchun/Linux_Code_Test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值