target_include_directories上的生成器表达式的用例是什么? [英] What is the use case for generator expression on target_include_directories?

查看:220
本文介绍了target_include_directories上的生成器表达式的用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个地方看到了在定义包含目录时使用生成器表达式的引用,因此您可以在编译和安装过程中为包含项定义不同的位置。例如:

I've seen in multiple places references to using generator expressions when defining include directories, so you can define different places for the includes during compilation and during installation. For example:

# Define headers for this library. PUBLIC headers are used for
# compiling the library, and will be added to consumers' build
# paths.
target_include_directories(lib PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
    PRIVATE src)

我正在构建一个图书馆项目,并且由于我使用的是标准路径( lib / 用于静态库,而 include / 用于公共头文件),我希望通过设置 CMAKE_INSTALL_PREFIX 来摆脱并使用简单的 install()调用,例如:

I'm building a library project and since I'm using standard paths (lib/ for the static library and include/ for the public headers), I was hoping to get away by setting CMAKE_INSTALL_PREFIX and using a simple install() call, such as:

set(CMAKE_INSTALL_PREFIX "${MY_INSTALL_DIR}")
install(TARGETS myLibrary ARCHIVE PUBLIC_HEADERS)

我的预期 DESTINATION 将是这两者的默认设置,所以我只是告诉CMake安装此类文件。当然这是行不通的,我需要显式设置库和头文件的目标。

My expectations were that the DESTINATION would be the default for both, so I'm just telling CMake to install these kind of files. Of course it doesn't work and I need to explicitly set the destination for both libraries and header files.

所以问题仍然存在:生成器的用例是什么?开头的表达式是否仍然无法使用 INSTALL_INTERFACE

So the question remains: what is the use case for the generator expressions at the beginning, if I don't seem to be able to use the INSTALL_INTERFACE anyway?

这是我的示例CMakeLists.txt:

This is my sample CMakeLists.txt:

cmake_minimum_required(VERSION 3.12.1)
project(my_library C)

FILE(GLOB SOURCE_FILES src/*.c)
add_library(my_library ${SOURCE_FILES})
target_include_directories(my_library PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>)

set(OUTPUT_DIR "${CMAKE_BINARY_DIR}/output")
set(INSTALL_DIR "${OUTPUT_DIR}/my_library")
set(INSTALL_LIB_DIR "${INSTALL_DIR}/lib")
set(INSTALL_INC_DIR "${INSTALL_DIR}/include")
set(CMAKE_INSTALL_PREFIX "${INSTALL_DIR}")
install(TARGETS my_library ARCHIVE DESTINATION lib)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include" DESTINATION include)
# I was hoping I could do, after setting the headers with the INSTALL_INTERFACE generator expression
#install(TARGETS my_library ARCHIVE PUBLIC_HEADERS)


推荐答案

类似于生成器的表达式<$ CMake使用c $ c> $< BUILD_INTERFACE> 和 $< INSTALL_INTERFACE> 来区分事物,对于 build来说有所不同

Generator-like expressions $<BUILD_INTERFACE> and $<INSTALL_INTERFACE> are used by CMake for distinguishing things, different for the build tree and for the install tree.

在构建项目本身时, $< BUILD_INTERFACE> 安装树被使用,但 $< INSTALL_INTERFACE> 不被使用。

When building the project itself, $<BUILD_INTERFACE> is used but $<INSTALL_INTERFACE> is not.

创建<带有 install(EXPORT)的strong>导出文件 a>命令, $< INSTALL_INTERFACE> 中列出的内容将包含在其中,但 $< BUILD_INTERFACE> 中的内容不会。

When creating an export file with the install(EXPORT) command, things listed in $<INSTALL_INTERFACE> will be included into it, but things in $<BUILD_INTERFACE> won't.

但是创建导出文件中使用 export 命令使用 $< BUILD_INTERFACE> ,但不使用 $< INSTALL_INTERFACE>

But the creation of an export file with the export command uses $<BUILD_INTERFACE>, but does not use $<INSTALL_INTERFACE>.

其他用于差异化的生成和安装树表达式 $< BUILD_INTERFACE> $< INSTALL_INTERFACE>

Other for differentiate build and install trees expressions $<BUILD_INTERFACE> and $<INSTALL_INTERFACE> are not used.

例如 $< INSTALL_INTERFACE> 确实影响 install(TARGETS .. PUBLIC_HEADERS)命令

这篇关于target_include_directories上的生成器表达式的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆