CMake中的add_compile_options命令用于向源文件的编译添加选项,其格式如下:
add_compile_options(
将选项添加到COMPILE_OPTIONS目录属性。从当前目录及以下目录编译target时,将使用这些选项。
add_compile_options命令的参数可以使用语法为$<...>的"生成器表达式"。
一个target所使用的最终选项集(final set of options)是通过累积当前target的选项和其依赖的使用要求(usage requirements)而构建的(constructed)。对选项集进行重复项删除(de-duplicated)以避免重复(repetition)。
由于不同的编译器支持不同的选项,这个命令的典型用法是在特定于编译器(compiler-specific)的条件子句中。
此命令可用于添加任何选项。但是,要添加预处理器定义和包含目录(preprocessor definitions and include directories),建议使用更具体的命令 add_compile_definitions和include_directories。
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}") # CMAKE_CXX_COMPILER_ID: GNU
add_compile_options(-DSAMPLE_ADD)
add_compile_options(-DSAMPLE_ADD_VALUE=10)
add_compile_options(-DTEST_OPTION)
# lots of warnings and all warnings as errors
add_compile_options(-Wall -Wextra -pedantic -Werror)
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 PRIVATE "DO_GNU_TESTS")
CMake中的target_compile_options命令用于向target添加编译选项,其格式如下:
target_compile_options( [BEFORE]
[items1...]
[ [items2...] ...])
向COMPILE_OPTIONS或INTERFACE_COMPILE_OPTIONS target属性添加选项。在编译给定的
如果指定BEFORE,则内容将被添加到该属性的前面(prepended to the property),而不是被追加。
INTERFACE, PUBLIC和PRIVATE关键字用于指定以下参数(following arguments)的作用域(scope).PRIVATE和PUBLIC项将填充
允许在IMPORTED target上设置INTERFACE项。
target_compile_options的参数可以使用语法为$<...>的"生成器表达式"。
一个target所使用的最终选项集(final set of options)是通过累积当前target的选项和其依赖的使用要求(usage requirements)而构建的(constructed)。对选项集进行重复项删除(de-duplicated)以避免重复(repetition)。
此命令可用于添加任何选项。但是,要添加预处理器定义和包含目录(preprocessor definitions and include directories),建议使用更具体的命令 target_compile_definitions和target_include_directories。
# reference: https://github.com/Kitware/CMake/tree/master/Tests/CMakeCommands/target_compile_options
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}") # CMAKE_CXX_COMPILER_ID: GNU
include_directories(include)
add_executable(main samples/sample_add.cpp)
target_compile_options(main
PRIVATE $<$:-DMY_PRIVATE_DEFINE>
PUBLIC $<$:-DMY_PUBLIC_DEFINE>
PUBLIC $<$:-DMY_MUTLI_COMP_PUBLIC_DEFINE>
INTERFACE $<$:-DMY_INTERFACE_DEFINE>
INTERFACE $<$:-DMY_MULTI_COMP_INTERFACE_DEFINE>
)
target_compile_options(main
PRIVATE
-DCONSUMER_LANG_$
-DLANG_IS_CXX=$
-DLANG_IS_C=$
)
target_compile_options(main
PUBLIC
-DSAMPLE_ADD
-DSAMPLE_ADD_VALUE=10
)
# test no items
target_compile_options(main PRIVATE)
target_compile_definitions(main PRIVATE "DO_GNU_TESTS2")
add_library(add STATIC source/add.cpp)
target_link_libraries(main add)
执行测试代码需要多个文件:
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_add_compile_options.cmake内容为上面的所有测试代码段
另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:
其中samples/sample_add.cpp内容如下:
#include
#include
#ifdef DO_GNU_TESTS
# ifndef TEST_OPTION
# error Expected TEST_OPTION
# endif
#endif
#ifdef DO_GNU_TESTS2
# ifndef CONSUMER_LANG_CXX
# error Expected CONSUMER_LANG_CXX
# endif
# ifdef CONSUMER_LANG_C
# error Unexpected CONSUMER_LANG_C
# endif
# if LANG_IS_C
# error Unexpected LANG_IS_C
# endif
# if !LANG_IS_CXX
# error Expected LANG_IS_CXX
# endif
# ifndef MY_PRIVATE_DEFINE
# error Expected MY_PRIVATE_DEFINE
# endif
# ifndef MY_PUBLIC_DEFINE
# error Expected MY_PUBLIC_DEFINE
# endif
# ifndef MY_MUTLI_COMP_PUBLIC_DEFINE
# error Expected MY_MUTLI_COMP_PUBLIC_DEFINE
# endif
# ifdef MY_INTERFACE_DEFINE
# error Unexpected MY_INTERFACE_DEFINE
# endif
#endif
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;
}
可能的执行结果如下图所示:
GitHub:https://github.com/fengbingchun/Linux_Code_Test