Cmake include_directories() [英] Cmake include_directories()

查看:1362
本文介绍了Cmake include_directories()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的项目树:

project
| + src
| + external
| | + foo
| | | + include
| | | | - foo.hpp
| | | + src
| | | | - foo.cpp
| | | | - CMakeLists.txt
| | | - CMakeLists.txt 
| | + CMakeLists.txt 
| + src
| | - main.cpp
| - CMakeLists.txt

foo.cpp包括foo.hpp:

foo.cpp includes foo.hpp:

// foo.cpp
#include "foo.hpp"

现在的问题是,包括顶部CMake中的目录成功找到foo.hpp,
,但如果我包含在子项目中,它不会。有什么原因吗? (编译可执行文件之前包含目录)。

Now the problem is that including the directory in the top CMake successfully find foo.hpp, but if I include in the subproject it doesn't. Any reason for it? (directories are included before the executable is compiled).

// project/CMakeLists.txt
include_directories(external/foo/include) //OK
add_subdirectory(external)

add_executable(main main.cpp)
target_link_libraries(main foo)

// project/external/CMakeLists.txt
add_subdirectory(foo)

// project/external/foo/CMakeLists.txt 
include_directories(include) // NOT WORKING
add_subdirectory(src)

// project/external/foo/src/CMakeLists.txt 
add_library(foo foo.cpp)


推荐答案

引用 include_directories


include目录添加到当前CMakeLists文件的目录属性
INCLUDE_DIRECTORIES。它们也是
添加到目标属性INCLUDE_DIRECTORIES为
中的每个目标当前CMakeLists文件。目标属性值是生成器使用的

The include directories are added to the directory property INCLUDE_DIRECTORIES for the current CMakeLists file. They are also added to the target property INCLUDE_DIRECTORIES for each target in the current CMakeLists file. The target property values are the ones used by the generators.

INCLUDE_DIRECTORIES 目录属性继承到所有子目录和所有目标

The INCLUDE_DIRECTORIES directory property is inherited to all subdirectories and all targets in the directory.


  • $ {CMAKE_CURRENT_SOURCE_DIR} > include_directories 是多余的,因为默认情况下相对路径被解释为相对于此目录。

  • 在子目录及其父目录中指定include目录是多余的。

  • 使用 get_property message 仔细检查所有目录和目标是否都在 INCLUDE_DIRECTORIES 属性。

  • 如果您可以自由要求CMake 2.8.11作为最低要求,请考虑完全放弃 include_directories 使用 target_include_directories 改为。这具有的优点是包括依赖性在每个目标基础上得到解决,这通常是更理想的行为。

  • Specifying ${CMAKE_CURRENT_SOURCE_DIR} for include_directories is redundant as relative paths are interpreted as relative to this directory by default. You should throw it out to increase readability.
  • Specifying an include directory in both a subdirectory and its parent is redundant. You should avoid this and settle on one location.
  • Use get_property and message to double-check that all directories and targets end up with the correct entries in their INCLUDE_DIRECTORIES property.
  • If you are free to require CMake 2.8.11 as a minimum requirement, consider abandoning include_directories completely and use target_include_directories instead. This has the advantage that include dependencies get resolved on a per-target basis exclusively which is usually the more desirable behavior.

这篇关于Cmake include_directories()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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