CMake中include的使用
Posted fengbingchun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CMake中include的使用相关的知识,希望对你有一定的参考价值。
CMake中的include命令用于从文件或模块(file or module)加载并运行CMake code。其格式如下:
include(<file|module> [OPTIONAL] [RESULT_VARIABLE <var>]
[NO_POLICY_SCOPE])
从给定的文件加载并运行CMake code。变量读写访问调用者的范围(Variable reads and writes access the scope of the caller (dynamic scoping))。
如果指定了OPTIONAL,即使文件不存在也不会触发error。
include(xxxx.cmake OPTIONAL) # xxxx.cmake不存在也不会触发warning或error
include(xxxx.cmake) # xxxx.cmake不存在,会触发error
# CMake Error at test_include.cmake:9 (include):
# include could not find requested file: xxxx.cmake
如果给定了RESULT_VARIABLE,变量<var>将被设置为已包含的完整的文件名,如果没有找到且指定了OPTIONAL则为NOTFOUND。
include(test_project.cmake RESULT_VARIABLE var)
message("var: $var") # var: /home/spring/GitHub/Linux_Code_Test/Samples_CMake/messy_usage/test_project.cmake
include(xxxx.cmake OPTIONAL RESULT_VARIABLE var) # xxxx.cmake不存在
message("var: $var") # var: NOTFOUND
include(xxxx.cmake RESULT_VARIABLE var) # xxxx.cmake不存在,触发error
# CMake Error at test_include.cmake:20 (include):
# include could not find requested file: xxxx.cmake
如果指定了module而不是file,则首先在CMAKE_MODULE_PATH中搜索名为<modulename>.cmake的文件,然后在CMake module目录(如/usr/share/cmake-3.22/Modules/)中搜索。有一个例外,如果调用include命令的文件本身位于CMake内置模块目录(builtin module directory)中,则首先搜索CMake内置模块目录,然后搜索CMAKE_MODULE_PATH。
include(FindCUDA) # 在/usr/share/cmake-3.22/Modules/目录下查找到
message("cmake module path: $CMAKE_MODULE_PATH") # cmake module path:
include(FindCUDA OPTIONAL RESULT_VARIABLE var)
message("var: $var") # var: /usr/share/cmake-3.22/Modules/FindCUDA.cmake
# 下载opencv 4.6.0并将其解压缩到/opt/opencv-4.6.0目录下
set(CMAKE_MODULE_PATH /opt/opencv-4.6.0/cmake)
message("cmake module path: $CMAKE_MODULE_PATH") # cmake module path: /opt/opencv-4.6.0/cmake
include(FindCUDA OPTIONAL RESULT_VARIABLE var)
message("var: $var") # var: /opt/opencv-4.6.0/cmake/FindCUDA.cmake
如果include命令读取的文件包含对cmake_policy命令的使用,则默认情况下该policy的设置不会影响调用者,可通过include命令中可选的NO_POLICY_SCOPE关键字来控制此行为。
cmake_policy(GET CMP0065 var) # 3.4
message("var: $var") # var: NEW
# 临时调整test_cmake_policy.cmake中的第3测试段,使其显示设置CMP0065为OLD
include(test_cmake_policy.cmake) # 不带NO_POLICY_SCOPE,在test_cmake_policy.cmake中对CMP0065设置为OLD时,在test_include.cmake中不起作用
cmake_policy(GET CMP0065 var) # 3.4
message("var: $var") # var: NEW
# 临时调整test_cmake_policy.cmake中的第3测试段,使其显示设置CMP0065为OLD
include(test_cmake_policy.cmake NO_POLICY_SCOPE) # 带NO_POLICY_SCOPE后,在test_cmake_policy.cmake中对CMP0065设置为OLD时,在test_include.cmake中也生效
cmake_policy(GET CMP0065 var) # 3.4
message("var: $var") # var: OLD
执行上述测试代码需要3个文件:build.sh, CMakeLists.txt, test_include.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)
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 ===="
cmake -DTEST_CMAKE_FEATURE=$1 ..
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_include.cmake:为上面的所有示例代码
可能的执行结果如下图所示:
GitHub: https://github.com/fengbingchun/Linux_Code_Test
以上是关于CMake中include的使用的主要内容,如果未能解决你的问题,请参考以下文章
CMake中target_include_directories的使用