CMake中的target_compile_definitions命令用于向target添加编译定义,其格式如下:
target_compile_definitions(<target>
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
指定在编译给定的<target>时要使用的编译定义。命名的<target>必须由诸如add_executable或add_library之类的命令创建,并且不能为ALIAS target。
需要INTERFACE, PUBLIC和PRIVATE关键字来指定以下参数(following arguments)的作用域(scope).PRIVATE和PUBLIC项将填充<target>的COMPILE_DEFINITIONS属性。PUBLIC和INTERFACE项将填充<target>的INTERFACE_COMPILE_DEFINITIONS属性。以下参数指定编译定义。重复调用相同的<target>会按调用顺序追加项。
允许在IMPORTED target上设置INTERFACE项。
target_compile_definitions的参数可以使用语法为$<...>的"生成器表达式"。
项(item)上的任何前导-D都将被删除。忽略空项(Empty items are ignored).
定义可以选择有值。
注意:许多编译器将-DFOO等同于-DFOO=1,但是其它工具可能无法在所有情况下都识别出此情况(例如IntelliSense)。
include_directories(include)
add_library(add STATIC source/add.cpp)
add_executable(main samples/sample_add.cpp)
target_link_libraries(main add)
target_compile_definitions(main PUBLIC SAMPLE_ADD)
target_compile_definitions(main PRIVATE SAMPLE_ADD_VALUE=10)
# 以下各项都是等效的
target_compile_definitions(main PUBLIC FOO)
target_compile_definitions(main PUBLIC -DFOO) # -D removed
target_compile_definitions(main PUBLIC "" FOO) # "" ignored
target_compile_definitions(main PUBLIC -D FOO) # -D becomes "", then ignored
执行测试代码需要多个文件:
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[@]}; 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_definitions.cmake内容为上面的所有测试代码段
另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:
其中samples/sample_add.cpp内容如下:
#include <iostream>
#include <add.hpp>
int main()
{
#ifdef SAMPLE_ADD
fprintf(stdout, "**** defined SAMPLE_ADD ****\n");
#endif
#if SAMPLE_ADD_VALUE == 10
fprintf(stdout, "#### defined SAMPLE_ADD_VALUE 10 ####\n");
#endif
int a = 2, b = 3;
fprintf(stdout, "%d+%d=%d\n", a, b, add(a,b));
return 0;
}
可能的执行结果如下图所示: