【问题标题】:CMakeLists include Header File from subdirectoriesCMakeLists 包含子目录中的头文件
【发布时间】:2021-01-07 13:23:16
【问题描述】:

我第一次尝试使用 CMake,并且正在努力将头文件链接到我的主文件中。我的 cmake 目录如下所示:

Project
| CmakeLists.txt
| src
|| CMakeLists.txt
|| Main.cpp
| Libs
|| CMakeLists.txt
|| headers 
|||obstacle_detection.hpp
||source
|||obstacle_detection.cpp
|build
||"build files"

我想将headers 文件夹中的文件链接到主目录,但我目前拥有的文件似乎不起作用。以下正确运行 CMake 命令,但无法使用 make 命令编译,无法找到给定的头文件。我的 CMakeLists 文件如下:

项目:

cmake_minimum_required(VERSION 3.17)
project(Sensivision)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") 
find_package(OpenCV REQUIRED)
find_package(realsense2 REQUIRED)
find_library(darknet REQUIRED)
add_subdirectory(libs)
add_subdirectory(src)
target_link_libraries(${PROJECT_NAME} obstacle_detection)

库:

add_library(
    obstacle_detection
    headers/obstacle_detection.hpp
    sources/obstacle_detection.cpp
)


target_link_directories(obstacle_detection PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

源代码:

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME} ${realsense2_LIBRARY})

我在 main.cpp 中的包含是

include <obstacle_detection.hpp>

我也试过

include <headers/obstacle_detection.hpp>

include <obstacle_detection>

每个都给出错误:

obstacle_detection.hpp: no such file or directory 

我在将标题链接到主时做错了什么?

【问题讨论】:

    标签: c++ cmake


    【解决方案1】:

    您尚未向obstacle_detection 库添加任何包含目录。通过在add_library 调用中列出头文件,这可能允许在IDE 中显示头文件,但它对编译没有任何作用。您应该使用target_include_directoriesheaders 目录添加为obstacle_detection 库的include 目录。否则,它和其他消费目标将不知道该目录中的标头。

    add_library(
        obstacle_detection
        headers/obstacle_detection.hpp
        sources/obstacle_detection.cpp
    )
    
    # Add this line. 
    target_include_directories(obstacle_detection PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/headers)
    
    # Not sure this line is necessary, as it doesn't appear you actually link anything...
    target_link_directories(obstacle_detection PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
    

    您尚未在 src 目录中显示 CMake 代码,但请务必将 obstacle_detection 库目标链接到主目标,例如:

    target_link_libraries(MyExeTarget PRIVATE obstacle_detection)
    

    另外,因为这个头文件是本地的,所以最好用引号把头包括进去:

    #include "obstacle_detection.hpp"
    

    【讨论】:

    • 谢谢这解决了我的问题。在这种情况下,包含和链接有什么区别?
    • @noble_gasses 头文件通常没有链接;相反,它们在编译过程中用于为您的类/变量/等提供定义。您可以使用 target_include_directories 在 CMake 中指定它。编译后,生成的目标文件(以及可能的其他现有库)在链接阶段得到链接以创建新库或可执行文件。您可以使用 target_link_libraries 指定其他库与 CMake 链接。请参阅回复here 了解不同阶段的一些概述。
    【解决方案2】:

    您可以使用target_include_directories 添加标题所在的文件夹,并在需要的地方使用#include &lt;header.hpp&gt;。 例如:

    libs cmake:
    add_library(
        obstacle_detection
        headers/obstacle_detection.hpp
        sources/obstacle_detection.cpp
    )
    target_include_directories(obstacle_detection PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
    
    cpp:
    #include <headers/obstacle_detection.hpp>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2016-11-04
      相关资源
      最近更新 更多