如何从 CMakeLists.txt 文件设置 include_directories? [英] How to set include_directories from a CMakeLists.txt file?

查看:34
本文介绍了如何从 CMakeLists.txt 文件设置 include_directories?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在使用 CMake 中的 include_directories() 命令设置包含路径 (-I) 时遇到问题.我的项目目录如下:

I seem to be having trouble setting the include path (-I) using the include_directories() command in CMake. My project directory is as follows:

Root
| - CMakeLists.txt
| - libs
| - | - CMakeLists.txt
| - | - inc
| - | - | - // lib specific includes
| - | - src
| - | - | - // lib specific sources
| - proj1
| - | - CMakeLists.txt
| - | - inc
| - | - | - // proj1 specific includes
| - | - src
| - | - | - // proj1 specific sources

CMakeLists.txt 文件看起来像这样:

The root CMakeLists.txt file looks like so:

project(ROOT)
add_subdirectory(libs)
add_subdirectory(proj1)

libs下的CMakeLists.txt文件:

project(lib)
add_library(lib STATIC ${lib_hdrs} ${lib_srcs}) // for conciseness, omitted set() 

最后,proj1 下的 CMakeLists.txt 文件:

And lastly, the CMakeLists.txt file under proj1:

project(proj1)

include_directories("${ROOT_SOURCE_DIR}/lib/inc") # <- problem line?

add_executable(proj1 ${proj1_srcs})
target_link_libraries(proj1 lib)

目标是从 libs 中的源文件和头文件创建库,然后链接到在 proj1 下生成的可执行文件.Proj1 有一些#include 在libs 中包含的文件,所以我需要添加要与-I 一起使用的目录.根据文档,这就是 include_directories() 应该做的.然而,尽管明确设置并在其后使用调试 message(${INCLUDE_DIRECTORIES})INCLUDE_DIRECTORIES 变量是一个空字符串,并且没有为包含路径指定目录,所以我编译 proj1 失败了.

The goal is to create the library from the source and header files in libs, then link against the executable generated under proj1. Proj1 has some files that #include stuff in libs include, so I need to add the directories to be used with -I. Based on the documentation, that's what include_directories() is supposed to do. However despite explicitly setting that and following it with a debug message(${INCLUDE_DIRECTORIES}), the INCLUDE_DIRECTORIES variable is an empty string, and no directories are specified for the include path, so my compilation of proj1 fails.

我还尝试删除 ${ROOT_SOURCE_DIR}/inc 周围的引号,看看是否有帮助,但没有运气.

I've also attempted removing the quotes around ${ROOT_SOURCE_DIR}/inc to see if that helped but no luck.

推荐答案

include_directories() 填充名为 INCLUDE_DIRECTORIES 的目录属性:

include_directories() populates a directory property called INCLUDE_DIRECTORIES:

http://www.cmake.org/cmake/help/v2.8.12/cmake.html#prop_dir:INCLUDE_DIRECTORIES

请注意,CMake 2.8.11 学习了 target_include_directories 命令,该命令填充了 INCLUDE_DIRECTORIES 目标属性.http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:target_include_directories

Note that CMake 2.8.11 learned the target_include_directories command, which populates the INCLUDE_DIRECTORIES target property. http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:target_include_directories

还要注意,您可以将要针对 lib 目标的标头进行编译,需要包含目录 lib/inc"这一事实编码到 lib 通过使用 target_include_directoriesPUBLIC 关键字来定位自身.

Note also that you can encode the fact that 'to compile against the headers of the lib target, the include directory lib/inc is needed' into the lib target itself by using target_include_directories with the PUBLIC keyword.

add_library(lib STATIC ${lib_hdrs} ${lib_srcs}) # Why do you list the headers?
target_include_directories(lib PUBLIC "${ROOT_SOURCE_DIR}/lib/inc")

还要注意,我假设您没有安装 lib 库供其他人使用.在这种情况下,您需要为构建位置和安装位置指定不同的头目录.

Note also I am assuming you don't install the lib library for others to use. In that case you would need to specify different header directories for the build location and for the installed location.

target_include_directories(lib
  PUBLIC
    # Headers used from source/build location:
    "$<BUILD_INTERFACE:${ROOT_SOURCE_DIR}/lib/inc>"
    # Headers used from installed location:
    "$<INSTALL_INTERFACE:include>"     
)

无论如何,这只有在您安装 lib 供他人使用时才重要.

Anyway, that's only important if you are installing lib for others to use.

在上面的 target_include_directories(lib ...) 之后,您不需要其他 include_directories() 调用.lib 目标告诉" proj1 它需要使用的包含目录.

After the target_include_directories(lib ...) above you don't need the other include_directories() call. The lib target 'tells' proj1 the include directories it needs to use.

另见target_compile_defintions()target_compile_options().

这篇关于如何从 CMakeLists.txt 文件设置 include_directories?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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