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.cmakeinclude(xxxx.cmake OPTIONAL RESULT_VARIABLE var) # xxxx.cmake不存在
message("var: ${var}") # var: NOTFOUNDinclude(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[@]}; 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 ===="
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的使用相关推荐

  1. CMake中include指令用法介绍

    转载于:  https://blog.csdn.net/liitdar/article/details/81144461 本文主要介绍CMake中include指令的用法. 1 概述 引用CMake官 ...

  2. coverage 覆盖多个测试文件时_奇技淫巧[2]:cmake中添加lcov代码覆盖测试

    奇技淫巧[2]:cmake中添加lcov代码覆盖测试 1 目的 为CMake工程的test添加lcov代码覆盖性测试 2 要点 添加lcov支持的方法应该有很多,比较方便的有: (1)利用脚本基于文件 ...

  3. CMake中file的使用

    CMake中的file命令用于文件操作,其文件格式如下:此命令专用于需要访问文件系统的文件和路径操作 Readingfile(READ <filename> <variable> ...

  4. CMake中foreach的使用

    CMake中的foreach命令为list中的每个值评估一组命令(Evaluate a group of commands for each value in a list),其格式如下:其中< ...

  5. CMake中option和cmake_dependent_option的使用

    CMake中的option命令为用户提供可以选择的布尔选项(boolean option),其格式如下: option(<variable> "<help_text> ...

  6. CMake中target_compile_features的使用

    CMake中的target_compile_features命令用向target添加预期的编译器功能(compiler features),其格式如下: target_compile_features ...

  7. CMake中macro的使用

    在https://blog.csdn.net/fengbingchun/article/details/127144948 中介绍了CMake中function的使用,这里介绍下macro的使用,它与 ...

  8. CMake中set/unset的使用

    CMake中的set命令用于将普通.缓存或环境变量(normal, cache, or environment variable)设置为给定值,其格式如下:指定<value>...占位符( ...

  9. CMake中link_directories/target_link_directories的使用

    CMake中的link_directories命令用于添加目录使链接器能在其查找库(add directories in which the linker will look for librarie ...

最新文章

  1. 完美解决百度地图MarkerClusterer 移动地图时,Marker 的Label 丢失的问题
  2. Python学习笔记:错误和异常
  3. 万万没想到,枯燥的“机器学习”还可以这样学!
  4. [基础题] 7.第二种(*)按如下要求编写Java程序:
  5. 网页设计布局选择:固定,流行和弹性布局 (2010-12-14 13:07:35)
  6. 黑马程序员——iOS学习——启动App界面黑屏
  7. 算法导论——基本的图算法
  8. 计算机科普小知识——U盘格式化
  9. 北风设计模式课程---创建模式、结构模式、行为模式的区别
  10. VisualAssistX中文注释提示错误 解决办法
  11. 图形 3.6 纹理压缩——包体瘦身术——RGBA与ASTC与ETC2压缩与实际对比体验
  12. 利用jink的驱动软件j-flash 合并两个hex的方法,bootloader+app -(转载)
  13. 计算机网络机房需要气体灭火吗,机房排烟和机房消防要求有哪些
  14. 南卡OE Pro上线!开放式耳机新里程碑!前所未有的音质舒适双冠
  15. 有农夫、猎人、挑夫、船夫每天都要走过一段索道
  16. VC++深入详解学习笔记
  17. MySQL主从复制bug记录
  18. 水晶头的制作的学习经历
  19. require() - NodeJS
  20. 5款国产ARM芯片(对标stm32f103c8t6)测试评估

热门文章

  1. Linux 音频player对比
  2. c 语言桌面应用开发,用Go语言开发桌面应用--By TSL
  3. qemu rtl8139调试问题
  4. 历史上伟大的数学家_数学历史上的10个尴尬时刻
  5. 法拉第未来再遭打击,被视觉效果公司索赔180万美元
  6. Infoblox DDI NIOS 8.5.2 -- DDI 核心网络服务管理
  7. NFT游戏初创公司Mythical Games旗下游戏将与巴宝莉等进行合作
  8. 足球游戏源码《沙滩足球》beach soccer源码H5+安卓+IOS三端源码
  9. win10IE浏览器只能以管理员身份才能运行,如何恢复
  10. 河南人,想说爱你不容易