我在 CMake 中有 2 个项目 L 和 A,假设 L 是一个库,A 是一个依赖于 L 的应用程序。
文件夹层次结构如下:
- 一个
- CMakeLists.txt + headers + src
- 大号
- CMakeLists.txt + headers + src
- 子L
- 标题+源文件
现在,就包含目录而言,我明白我需要在 L/CMakeLists.txt/include_directories() 中指定 subL,这样 L 中的其他文件可以引用包含文件只需使用 #include "mySubLHeaderFile.h" 或 #include < mySubLHeaderFile.h>。
现在,如果我想在 A 项目的文件中引用来自 subL 的头文件,并且能够引用此 subL 文件只需使用 #include "mySubLHeaderFile.h" 或 #include < mySubLHeaderFile.h>,我注意到我需要重新指定 subL在 A/CMakeLists.txt/include_directories() 中。这是正常行为吗? include_directories() 不是继承自依赖项目吗?
------ 编辑 1 ------
请注意,我在 A 和 L 之上有一个顶级 CMakeLists.txt。
最佳答案
是的,这是 CMake 的 include_directories
命令的标准行为。如果您希望将属性传播给依赖项,请使用 target_include_directories
命令成为版本 3 的标准。具体来说,客户端代码所需的 header 位置应位于 PUBLIC
或 INTERFACE
部分。如果 SubL 还需要在那里包含 header ,请使用 PUBLIC
。查看用于将属性传播到依赖项的整个 target_*
命令系列。
关于c++ - CMake 中的 include_directory 继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229198/