CMake在子模块B中不包含子模块A的头文件目录 [英] CMake doesn't include header directory of submodule A within submodule B

查看:443
本文介绍了CMake在子模块B中不包含子模块A的头文件目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的CMake项目:

I have a CMake project that looks like this:

project/
  CMakeLists.txt
  subprojectA/
    CMakeLists.txt
    include/
      headerA.hpp
    src/
      libraryA.cpp
  subprojectB/
    CMakeLists.txt
    src/
      mainB.cpp

库子项目A已编译作为静态库,成为libsubprojectA.a。 主项目B被编译为二进制文件,并依赖于库。 mainp.cpp包含对headerA.hpp的引用。

The "library" subproject, A, is compiled as a static library, becoming libsubprojectA.a. The "main" project, B, is compiled as a binary and depends on the library. mainB.cpp includes a reference to headerA.hpp.

这是subprojectA / CMakeLists.txt:

Here is subprojectA/CMakeLists.txt:

project(SubProjectA)
include_directories(include)
add_library(subprojectA STATIC src/libraryA.cpp)
set(${PROJECT_NAME}_INCLUDE_DIRS
    ${PROJECT_SOURCE_DIR}/include
    CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)

这是subprojectB / CMakeLists.txt:

And here is subprojectB/CMakeLists.txt:

project(SubProjectB)
include_directories(${SubProjectA_INCLUDE_DIRS})
add_executable(mainBinary src/mainB.cpp)
target_link_libraries(mainBinary subprojectA)

主项目CMakeLists.txt如下:

The main Project CMakeLists.txt looks like:

project(Project)
add_subdirectory(subprojectB)
add_subdirectory(subprojectA)

请注意,子项目B(主要项目)列在之前子项目

Note that subprojectB, the main project, is listed before subprojectA.

这是问题所在。当我第一次在该项目上运行 cmake时,在SubProjectB中未设置 $ {SubProjectA_INCLUDE_DIRS}

Here's the problem. When I first run "cmake" on this project, ${SubProjectA_INCLUDE_DIRS} is not set within SubProjectB.

什么我认为正在发生的情况是,尚未设置 $ {SubProjectA_INCLUDE_DIRS} 时,SubProjectB的CMakeLists首先加载。结果将它自己的包含路径设置为空字符串。但是,即使 libsubprojectA.a mainBinary 之前已成功构建,但include路径已经预先设置为空。结果,在尝试制作 mainBinary 时出现此错误:

What I think is happening is that the CMakeLists for SubProjectB loads first, when ${SubProjectA_INCLUDE_DIRS} has not yet been set. It sets its own include path to an empty string as a result. However, even though libsubprojectA.a gets built successfully before mainBinary, the include path was already set empty beforehand. As a result, I get this error when trying to make mainBinary:

subprojectB/src/mainB.cpp:1:23: fatal error: headerA.hpp: No such file or directory
#include "headerA.hpp"
                      ^

这是在CMake声明性世界的主项目CMakeList中将subprojectA放在subprojectB之前的解决方法。我真正想要的是知道向CMake表示 include_directories($ {SubProjectA_INCLUDE_DIRS})取决于的正确方法在SubProjectA的CMakeLists中。有没有更好的方法?

It's a workaround to put subprojectA before subprojectB in the main Project CMakeLists in the declarative world of CMake. What I really want is to know the proper way to indicate to CMake that the include_directories(${SubProjectA_INCLUDE_DIRS}) line depends on the definitions that exist inside SubProjectA's CMakeLists. Is there a better way to do this?

推荐答案

如果要表达包含目录 subprojectA / include 是库 subprojectA 接口,请使用 target_include_directories 命令:

If you want to express that include directory subprojectA/include is an interface of the library subprojectA, attach this property to the target with target_include_directories command:

subprojectA / CMakeLists.txt

project(SubProjectA)
add_library(subprojectA STATIC src/libraryA.cpp)
# PUBLIC adds both:
#     1) include directories for compile library and
#     2) include directories for library's interface
target_include_directories(subprojectA PUBLIC include)

因此与 subprojectA 链接的任何可执行文件(或其他库)将自动具有该包含目录:

So any executable(or other library) which linked with subprojectA will have this include directory automatically:

subprojectB / CMakeLists.txt

project(SubProjectB)
add_executable(mainBinary src/mainB.cpp)
target_link_libraries(mainBinary subprojectA)

当然,要正确使用最后一条命令,您需要使用库处理目录,然后使用可执行文件:

Of course, for use last command properly you need to process directory with library before one with executable:

CMakeLists.txt

project(Project)
add_subdirectory(subprojectA)
add_subdirectory(subprojectB)

这篇关于CMake在子模块B中不包含子模块A的头文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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