【问题标题】:using cmake to make header files descendants of the project's source directory使用 cmake 制作项目源目录的头文件后代
【发布时间】:2016-12-09 21:02:36
【问题描述】:

正如Google C++ Style Guide 中提到的,项目的所有头文件都应列为项目源目录的后代,而不使用 UNIX 目录快捷方式。 (当前目录)或 ..(父目录)。我该如何在下面简要描述的项目中做到这一点。

我的项目目录层次结构是这样的:

  • 图形引擎
    • header_files.h
    • source_files.cc
    • CMakeLists.txt (1)
      • CMakeLists.txt (2)
      • header_files.h
      • source_files.cc
    • 相机
      • CMakeLists.txt (3)
      • header_files.h
      • source_files.cc
    • 核心
      • CMakeLists.txt (4)
      • header_files.h
      • source_files.cc

这些是 CMakeLists.txt 文件的内容:

CMakeLists.txt (1)

add_library(GraphicsEngineLib source_files.cc)
target_link_libraries(GraphicsEngineLib LightLib CameraLib CoreLib)
add_subdirectory(Light)
add_subdirectory(Camera)
add_subdirectory(Core)

CMakeLists.txt (2)

add_library(LightLib source_files.cc)

CMakeLists.txt (3)

add_library(CameraLib source_files.cc)

CMakeLists.txt (4)

add_library(CoreLib source_files.cc)

现在,例如,当我想将 Camera 文件夹中的头文件包含到 Core 文件夹中的文件中时,我必须使用 ../Camera/header_file.h 但我想使用 GraphicsEngine/Camera/header_file.h。我该怎么做?

【问题讨论】:

    标签: c++ cmake


    【解决方案1】:

    我过去所做的是将其设置在顶级CMakeLists.txt(应该在您的GraphicsEngine目录中):

    SET(PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/..")
    
    INCLUDE_DIRECTORIES(
      ${PROJECT_ROOT}
    )
    

    根据thisCMAKE_CURRENT_SOURCE_DIR在哪里

    这是当前处理的CMakeLists.txt所在目录

    请注意,通过以这种方式定义Project_Root,您的GraphicsEngine 项目也可以将姐妹 项目中的#include 标头发送到GraphicsEngine

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多