CMake中target_compile_features的使用

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

target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> <feature> [...])

      指定在编译给定的<target>时所需的编译器功能。如果该功能(feature)未在CMAKE_C_COMPILE_FEATURES,CMAKE_CUDA_COMPILE_FEATURES或CMAKE_CXX_COMPILE_FEATURES变量中列出,则CMake将报告错误。如果该功能的使用需要额外的编译器标志(compiler flag),例如-std=gnu++11,则会自动添加该标志。
      需要INTERFACE, PUBLIC和PRIVATE关键字来指定功能的作用域(scope of the features).PRIVATE和PUBLIC项将填充<target>的COMPILE_FEATURES属性。PUBLIC和INTERFACE项将填充<target>的INTERFACE_COMPILE_FEATURES属性。对相同的<target>重复调用会追加项(Repeated calls for the same <target> append items)。
      允许在IMPORTED target上设置INTERFACE项。
      命名的<target>必须由诸如add_executable或add_library之类的命令创建,并且不能为ALIAS target
      target_compile_features的参数可以使用语法为$<...>的"生成器表达式"。

message("#### CMAKE_CXX_COMPILE_FEATURES: ${CMAKE_CXX_COMPILE_FEATURES}") # #### CMAKE_CXX_COMPILE_FEATURES:  cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates; ...

if(cxx_std_11 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
    include_directories(include)

    add_executable(main samples/sample_add.cpp)
    target_compile_features(main PRIVATE cxx_std_11)

    add_library(add STATIC source/add.cpp)
    target_compile_features(add PUBLIC cxx_std_11)
    target_link_libraries(main add)
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 target_compile_features \
		add_compile_options target_include_directories link_directories)

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

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

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

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值