CMake中target_include_directories命令用于向target中添加包含目录,其格式如下:
target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
指定在编译给定target时要使用的包含目录。命名的<target>必须由诸如add_executable或add_library之类的命令创建,并且不能为ALIAS target。
通过显式使用AFTER或BEFORE,你可以独立于默认值在追加和前置(appending and prepending)之间进行选择。
INTERFACE,PUBLIC和PRIVATE关键字用于指定以下参数的作用域(scope)。PRIVATE和PUBLIC项将填充<target>的INCLUDE_DIRECTORIES属性。PUBLIC和INTERFACE项将填充<target>的INTERFACE_INCLUDE_DIRECTORIES属性。下面的参数指定包含目录。
允许在IMPORTED targets上指定INTERFACE项。
重复调用相同的<target>会按调用顺序追加项。
如果指定了SYSTEM,编译器将被告知该目录在某些平台上是系统包含目录(system include directories)。这可能会产生诸如抑制警告或跳过依赖计算中包含的头等影响。此外,无论指定的顺序如何,系统包含目录都在正常(normal)包含目录之后进行搜索。
如果SYSTEM与PUBLIC或INTERFACE一起使用,则INTERFACE_SYSTEM_INCLUDE_DIRECTORIES target属性将填充指定的目录。
target_include_directories的参数可以使用语法为$<...>的"生成器表达式"。
指定的包含目录可以是绝对路径或相对路径。相对路径将被解释为相对于当前源目录(即CMAKE_CURRENT_SOURCE_DIR),并在存储到关联的target属性之前转换为绝对路径。如果路径以生成器表达式开始,则始终假定它是绝对路径(下面指出一个例外)并且将不加修改地使用。
包含目录的使用要求在构建树和安装树(the build-tree and the install-tree)之间通常有所不同。BUILD_INTERFACE和INSTALL_INTERFACE生成器表达式可用于根据使用位置描述单独的使用要求。 INSTALL_INTERFACE表达式中允许使用相对路径,并将其解释为相对于安装前缀。相对路径不应在BUILD_INTERFACE表达式中使用,因为它们不会被转换为绝对路径。
创建可重定位的包(Creating Relocatable Packages):注意,不建议使用包含依赖项的目录的绝对路径填充target的INTERFACE_INCLUDE_DIRECTORIES的 INSTALL_INTERFACE。
add_executable(main samples/sample_subtraction.cpp)
target_include_directories(main PUBLIC include)
add_library(subtraction source/subtraction.cpp)
target_include_directories(subtraction PRIVATE include)
target_link_libraries(main subtraction)
# test no items
target_include_directories(main PRIVATE)
target_include_directories(main BEFORE PRIVATE)
target_include_directories(main SYSTEM BEFORE PRIVATE)
target_include_directories(main SYSTEM PRIVATE)
执行测试代码需要多个文件:
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_include_directories.cmake内容为上面的所有测试代码段
另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:
可能的执行结果如下图所示:
GitHub: https://github.com/fengbingchun/Linux_Code_Test