CMake如何包括没有源头? [英] CMake how to include headers without sources?

查看:87
本文介绍了CMake如何包括没有源头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个虚拟的问题,但我从字面上看google的前两个页面都没有成功。

我正在编写仅标头的库,但无法正确设置CMake配置,以便在构建解决方案时给定的 main.cpp 找到正确的包含项。

如何实现?

This is probably a dummy question but I have literally looked at the two first pages of google without success.
I'm writing a header only library and I'm unable to set up correctly the CMake configuration in order that when I build my solution a given main.cpp finds the proper includes.
How can this be accomplished?

编辑

所以我可能应该给出更详细的解释。

假设我有一个 ./ src 文件夹,其中: ./ src / core ./src/wrappers 。在每个文件夹中,我都有 .h 文件,这些文件需要包含在 main.cpp 文件中:

So I probably should give a little more detailed explanation.
Lets say I have a ./src folder with: ./src/core and ./src/wrappers. Inside each folder I have .h files that needs to be included in a main.cpp file:

#include <src/core/reader.h>

当我放入 CMakeList.txt 时仍然如此

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

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

I receive a message like: src/core/reader.h no such file or directory.

推荐答案

要使用该路径,您应该引用<$ c $的父目录c> src 。

假设顶级 CMakeLists.txt 处于同一级别src ,您可以改用它:

To be able to use that path, you should refer to the parent directory of src.
Assuming the top level CMakeLists.txt is at the same level of src, you can use this instead:

include_directories(${CMAKE_SOURCE_DIR})

根据 CMAKE_SOURCE_DIR 的文档:


到源树顶层的路径。

The path to the top level of the source tree.

如果 src 直接位于顶层目录中,则应该使用类似以下的内容:

If src is directly in the top level directory, this should let you use something like:

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






不过,有两个建议:


That said, a couple of suggestions:


  • 我宁愿添加以下内容:

  • I would rather add this:

include_directories(${CMAKE_SOURCE_DIR}/src)

并使用以下代码:

#include <whatever/you/want.h>

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

No longer src in your paths and restricted search area.

我可能会使用 target_include_directories 代替 include_directories 并指定用于该规则的 target

I would probably use target_include_directories instead of include_directories and specify the target to be used for that rules:

target_include_directories(main ${CMAKE_SOURCE_DIR}/src)

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

This must be put after the add_executable, otherwise the target is not visible.

这篇关于CMake如何包括没有源头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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