CMake中的set_target_properties命令用于为targets设置属性(targets can have properties that affect how they are built),其格式如下:
set_target_properties(target1 target2 ...
PROPERTIES prop1 value1
prop2 value2 ...)
该命令的语法是列出要更改的所有targets,然后提供接下来要设置的值。你可以使用任何所需的prop值对,然后使用get_property或 get_target_property命令将其提取。
CMake中的get_target_property命令用于从target获取属性,其格式如下:
get_target_property(<VAR> target property)
属性的值存储在变量<VAR>中。如果没有找到target属性,则行为取决于是否已将其定义为INHERITED属性。非继承(inherited)的属性将设置<VAR>为<VAR>-NOTFOUND,而继承的属性将搜索相关的父作用域(parent scope),如define_property所述,如果仍然无法找到属性,<VAR>将被设置为空字符串。
使用set_target_properties来设置target属性值。属性通常用于控制如何构建target,但有些属性用于查询target。该命令可以获取到目前为止创建的任何target的属性。targets不需要在当前CMakeLists.txt文件中。
include_directories(include)
add_library(add STATIC source/add.cpp)
add_library(subtraction SHARED source/subtraction.cpp)
add_library(multipy OBJECT source/multipy.cpp)
set_target_properties(add subtraction multipy PROPERTIES _ADD_ static _SUBTRACTION_ shared _MULTIPY_ object)
get_target_property(var add _ADD_)
message("var: ${var}") # var: static
get_target_property(var subtraction _SUBTRACTION_)
message("var: ${var}") # var: shared
get_target_property(var multipy _MULTIPY_)
message("var: ${var}") # var: object
get_target_property(var add XXXX)
message("var: ${var}") # var: var-NOTFOUND
执行测试代码需要多个文件:
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)
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
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_set_target_properties.cmake内容为上面的所有测试代码段
另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:
可能的执行结果如下图所示:
GitHub: https://github.com/fengbingchun/Linux_Code_Test