CMake target_include_directories 范围的含义 [英] CMake target_include_directories meaning of scope

查看:39
本文介绍了CMake target_include_directories 范围的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与CMake的target_include_directories相关的关键字PUBLICPRIVATEINTERFACE是什么意思?

What is the meaning of the keyword PUBLIC, PRIVATE, and INTERFACE related to CMake's target_include_directories?

推荐答案

这些关键字用于告诉何时需要传递给目标的包含目录列表.何时表示是否需要这些包含目录:

These keywords are used to tell when the list of include directories you're passing to the target are needed. By when, it means if those include directories are needed:

  • 编译该目标本身.
  • 编译依赖于该目标的其他目标(例如使用其公共标头).
  • 在上述两种情况下.

当 CMake 编译目标时,它使用目标 INCLUDE_DIRECTORIESCOMPILE_DEFINITIONSCOMPILE_OPTIONS 属性.当您在 target_include_directories() 等中使用 PRIVATE 关键字时,您会告诉 CMake 填充这些目标属性.

When CMake is compiling a target, it uses the targets INCLUDE_DIRECTORIES, COMPILE_DEFINITIONS, and COMPILE_OPTIONS properties. When you use the PRIVATE keyword in target_include_directories() and alike, you tell CMake to populate those target properties.

当 CMake 检测到目标 A 和另一个目标 B 之间的依赖关系时(例如当您使用 target_link_libraries(AB) 命令时),它会传递性地传播 B A 目标的使用要求.那些目标使用要求是依赖于B的任何目标必须满足的包含目录、编译定义等.它们由上面列出的属性的 INTERFACE_* 版本指定(如 INTERFACE_INCLUDE_DIRECTORIES),并在调用target_*() 命令.

When CMake detects a dependency between a target A and another target B (like when you use the target_link_libraries(A B) command), it transitively propagates B usage requirements to the A target. Those target usage requirements are the include directories, compile definitions, etc. that any target that depends on B must meet. They are specified by the INTERFACE_* version of the properties listed above (like INTERFACE_INCLUDE_DIRECTORIES), and are populated by using the INTERFACE keyword when calling the target_*() commands.

PUBLIC 关键字的意思大致是PRIVATE + INTERFACE.

The PUBLIC keyword means roughly PRIVATE + INTERFACE.

因此,假设您正在创建一个使用一些 Boost 标头的库 A.你会这样做:

Therefore, suppose you are creating a library A that uses some Boost headers. You would do:

  • target_include_directories(A PRIVATE ${Boost_INCLUDE_DIRS}) 如果您只在源文件 (.cpp) 或私有头文件 (.h).
  • target_include_directories(A INTERFACE ${Boost_INCLUDE_DIRS}) 如果您不在源文件中使用那些 Boost 标头(因此,不需要它们来编译 A).我实在想不出一个真实的例子.
  • target_include_directories(A PUBLIC ${Boost_INCLUDE_DIRS}) 如果你在公共头文件中使用那些 Boost 头文件,它们都包含在一些 A 的源文件中并且也可能包含在您的 A 库的任何其他客户端中.
  • target_include_directories(A PRIVATE ${Boost_INCLUDE_DIRS}) if you only use those Boost headers inside your source files (.cpp) or private header files (.h).
  • target_include_directories(A INTERFACE ${Boost_INCLUDE_DIRS}) if you don't use those Boost headers inside your source files (therefore, not needing them to compile A). I can't actually think of a real-world example for this.
  • target_include_directories(A PUBLIC ${Boost_INCLUDE_DIRS}) if you use those Boost headers in your public header files, which are included BOTH in some of A's source files and might also be included in any other client of your A library.

CMake 3.0 文档对此有更多详细信息构建规范和使用要求 属性.

CMake 3.0 documentation has more details on this build specification and usage requirements properties.

这篇关于CMake target_include_directories 范围的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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