CMake中的define_property命令用于定义和记录自定义属性,其格式如下:

define_property(<GLOBAL | DIRECTORY | TARGET | SOURCE |TEST | VARIABLE | CACHED_VARIABLE>PROPERTY <name> [INHERITED][BRIEF_DOCS <brief-doc> [docs...]][FULL_DOCS <full-doc> [docs...]][INITIALIZE_FROM_VARIABLE <variable>])

在作用域内定义一个属性以与set_property和get_property命令一起使用它主要用于定义属性初始化或继承(initialized or inherited)的方式。从历史上看,该命令还将文档(documentation )与属性关联,但这不再被视为主要用例。
      第一个参数决定了该属性被使用的作用域类型(kind of scope)。它必须是以下之一:

GLOBAL    = associated with the global namespace
DIRECTORY = associated with one directory
TARGET    = associated with one target
SOURCE    = associated with one source file
TEST      = associated with a test named with add_test
VARIABLE  = documents a CMake language variable
CACHED_VARIABLE = documents a CMake cache variable

注意:与set_property和get_property命令不同,不需要给出实际作用域;只有作用域类型是重要的。
      必需的PROPERTY选项紧跟要定义的属性的名称。
      如果给出了INHERITED选项,则当请求的属性(requested property)未在给定命令的作用域内设置时,get_property命令将链接到下一个更高的作用域。
      注意:此作用域链接行为仅适用于对get_property, get_directory_property, get_target_property, get_source_file_property和get_test_property命令的调用。设置属性时没有继承行为,因此将APPEND或APPEND_STRING和set_property命令一起使用在计算要追加到的内容时不会考虑继承的值。
      BRIEF_DOCS和FULL_DOCS选项后跟与属性关联的字符串,作为其简短和完整的文档(brief and full documentation).CMake不使用此文档,只是通过get_property命令的相应选项将其提供给项目(project).
      INITIALIZE_FROM_VARIABLE选项指定初始化属性的变量。它只能用于target属性。<variable>名称必须以属性名称结尾,并且不能以CMAKE_或_CMAKE_开头。属性名称必须至少包含一个下划线。建议属性名称具有特定于项目的前缀。

# 3.23版本BRIEF_DOCS和FULL_DOCS选项是可选的
define_property(GLOBAL PROPERTY pro_globalBRIEF_DOCS "A test property"FULL_DOCS "A long description of this test property"
)if(pro_global)message("define property") # won't print
endif()get_property(global_result GLOBAL PROPERTY pro_global DEFINED)
if(global_result)message("global property: ${global_result}") # global property: 1
endif()define_property(TARGET PROPERTY pro_targetBRIEF_DOCS "A test property"FULL_DOCS "A long description of this test property"
)get_property(target_result TARGET PROPERTY pro_target DEFINED)
if(target_result)message("target property: ${target_result}") # target property: 1include_directories(include)add_library(add STATIC source/add.cpp)set_target_properties(add PROPERTIES pro_target xxxx)get_target_property(var add pro_target)message("var: ${var}") # var: xxxx
endif()

执行测试代码需要多个文件

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 while return \math file configure_file \include_directories add_executable add_library target_link_libraries install \target_sources add_custom_command add_custom_target \add_subdirectory aux_source_directory \set_property set_target_properties define_property \add_definitions target_compile_definitions)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
make
# make install # only used in cmake files with install command

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_define_property.cmake内容为上面的所有测试代码段

另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:

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

GitHub: https://github.com/fengbingchun/Linux_Code_Test

CMake中define_property的使用相关推荐

  1. CMake中target_compile_features的使用

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

  2. CMake中link_directories/target_link_directories的使用

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

  3. CMake中add_compile_options/target_compile_options的使用

    CMake中的add_compile_options命令用于向源文件的编译添加选项,其格式如下: add_compile_options(<option> ...) 将选项添加到COMPI ...

  4. CMake中target_compile_definitions的使用

    CMake中的target_compile_definitions命令用于向target添加编译定义,其格式如下: target_compile_definitions(<target>& ...

  5. CMake中add_definitions/add_compile_definitions的使用

    CMake中的add_definitions命令用于在源文件的编译中添加-D定义标志,其格式如下: add_definitions(-DFOO -DBAR ...) 将当前目录中的target的定义添 ...

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

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

  7. CMake 中的 PUBLIC,PRIVATE,INTERFACE

    一.概述 CMake中经常会使用 target_**() 相关命令,target_**() 命令支持通过 PUBLIC,PRIVATE 和 INTERFACE 关键字来控制传播.本文主要介绍下这三个关 ...

  8. cmake中的变量和命令的大小写

    1 cmake中要特别注意命令和变量的大小写 2 cmake的内置命令是不区分大小写的 3 cmake内置变量是区分大小写的,或者干脆就说,cmake的所有变量都是区分大小写的 这就是变量和命令的不同 ...

  9. CMake中的ARGC ARGV 和ARGN参数意义

    编写或者查看一些开源cmake文件中经常遇到在宏(macro)和函数中经常会遇到ARGC ARGV 和ARGN等参数,该参数为cmake中专用变量,分别表示宏或者函数参数中的特殊意义. ARGC AR ...

最新文章

  1. HDU 1301 Jungle Roads(裸最小生成树)
  2. android 按钮中断,android – 如何“中断”在AccessibilityService中执行的操作?
  3. 如何安全使用计算机,如何安全的使用计算机
  4. verilog学习记(加法器)
  5. 【数据分享】学生受欢迎程度评价数据集
  6. 阿里云服务器Centos7 安装 pycuda报错:Could not build wheels for pycuda which use PEP 517 and cannot be install
  7. 李宏毅机器学习Lesson2——Logistic Regression实现收入预测
  8. 是否优化更新主题浏览量:_主题306:能力规划
  9. 对硬盘进行分区时,GPT和MBR有什么区别
  10. NPN和PNP三极管做开关电路
  11. 阿尔伯塔大学 计算机科学,阿尔伯塔大学计算机科学专业入学要求及申请费用盘点...
  12. too few variables specified 怎么解决
  13. Java开发常用在线工具
  14. modprobe命令用于智能地向内核中加载模块或者从内核中移除模块
  15. Java 假设一张足够大的纸,纸张的厚度为0.5 毫米。请问对折多少次以后,可以达到珠穆朗玛峰的高度(最新数据:8844.43 米)。请编写程序输出对折次数
  16. 《变量:大国的腾挪》摘记
  17. MP4学习(五)ts-mp4源码阅读(3)ftyp box的解析
  18. 华为 BGP认证功能
  19. keyshot渲染图文教程_Keyshot 渲染视频教程 入门到精通实用实例教材全套
  20. 需求分析系列:软件需求分析方法论

热门文章

  1. MySQL数据库索引组合索引的最左优先原则
  2. flyme8会更新Android版本吗,魅族正式宣布8款老机型升级Flyme8内测新版本,有你的手机吗?...
  3. 【BZOJ4247】挂饰
  4. 层次聚类优化方法BIRCH(聚类特征树的多阶段聚类)
  5. 【GIS开发】OpenLayers在线瓦片数据源汇总
  6. Cmder下载安装和使用ssh远程连接linux
  7. Zuul和Gateway请求IO模型比对(WebFlux优势)以及Reactor模型分析
  8. matlab中绘制牟合方盖,牟合方盖 - calculus的日志 - 网易博客
  9. Android--OpenGL坐标系
  10. Arduino遍历字符串