gpt4 book ai didi

c++ - CMake如何包含没有源代码的标题?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:44 25 4
gpt4 key购买 nike

这可能是一个伪问题,但我确实查看了 google 的前两个页面但没有成功。
我正在编写一个仅 header 库,我无法正确设置 CMake 配置,以便在我构建我的解决方案时,给定的 main.cpp 找到正确的包含。
如何实现?

编辑

所以我可能应该给出更详细的解释。
假设我有一个 ./src 文件夹,其中包含:./src/core./src/wrappers。在每个文件夹中,我都有 .h 文件需要包含在 main.cpp 文件中:

#include <src/core/reader.h>

当我输入 CMakeList.txt 时仍然是这样的:

include_directories(src/core)
add_executable(main main.cpp)

我收到如下消息:src/core/reader.h 没有这样的文件或目录。

最佳答案

为了能够使用该路径,您应该引用 src 的父目录。
假设顶级 CMakeLists.txtsrc 处于同一级别,您可以改用它:

include_directories(${CMAKE_SOURCE_DIR})

来自 CMAKE_SOURCE_DIR 的文档:

The path to the top level of the source tree.

如果 src 直接在顶级目录中,这应该让你使用类似的东西:

#include <src/whatever/you/want.h>

也就是说,一些建议:

  • 我宁愿添加这个:

    include_directories(${CMAKE_SOURCE_DIR}/src)

    然后使用这个:

    #include <whatever/you/want.h>

    不再 src 在您的路径和受限搜索区域中。

  • 我可能会使用 target_include_directories 而不是 include_directories 并指定要用于该规则的目标:

    target_include_directories(main ${CMAKE_SOURCE_DIR}/src)

    这必须放在add_executable之后,否则目标不可见。

关于c++ - CMake如何包含没有源代码的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40223903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com