简体   繁体   English

cmake 只包含头文件库

[英]cmake include header only library

I want to include spdlog into one of my project.我想将spdlog包含到我的项目之一中。 It is a header only library.它是一个只有头文件的库。 The project that i am building is using cmake.我正在构建的项目正在使用 cmake。 Currently i am using目前我正在使用

include_directories('src/logger/spdlog/')

in cmake and including the library as在 cmake 中并将库包括为

#include <spdlog/spdlog.h>

in logs.h inside logger folder.在 logger 文件夹内的 logs.h 中。 I am getting fatal error no such file or directory.我收到致命错误没有这样的文件或目录。 What is the correct way to include the same library in my application.在我的应用程序中包含相同库的正确方法是什么。

You are probably off one directory.你可能离开了一个目录。 Try either尝试

 include_directories("src/logger")

in the CMakeLists.txt , orCMakeLists.txt ,或

 #include <spdlog.h>

in the source code.在源代码中。

Cmake provides interface library specifically for "header-only library". Cmake 提供了专门用于“仅头文件库”的接口库。 I would suggest to do the following:我建议执行以下操作:

  1. Create a file structure like the following创建如下文件结构
third-party\spdlog\include
  1. Git clone the spdlog repository into the include directory with the name spdlog Git 将 spdlog 存储库克隆到名为spdloginclude目录中
  2. Create third-party\\spdlog\\CMakeLists.txt with the following content使用以下内容创建third-party\\spdlog\\CMakeLists.txt
find_package(Threads REQUIRED)
add_library(${TARGET_LOGGER} INTERFACE)
# add_library(spdlog::spdlog_header_only ALIAS ${TARGET_LOGGER})
target_include_directories(${TARGET_LOGGER} INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
                                                        "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(${TARGET_LOGGER} INTERFACE Threads::Threads)
  1. Define TARGET_LOGGER in the CMakeLists.txt of your project.在项目的 CMakeLists.txt 中定义 TARGET_LOGGER。 Link this target to your project with target_link_libraries(${TARGET_PROJECT} LINK_PUBLIC ${TARGET_LOGGER}) .使用target_link_libraries(${TARGET_PROJECT} LINK_PUBLIC ${TARGET_LOGGER})将此目标链接到您的项目。 Also don't forget to也不要忘记
add_subdirectory(
    third-party\spdlog
  )

Then, in your project, you can use #include "spdlog/spdlog.h" to include the library然后,在您的项目中,您可以使用#include "spdlog/spdlog.h"来包含库

First, don't use single quotes ' but double quotes " or just plain strings if they don't contain spaces.首先,不要使用单引号'而是双引号"或者如果它们不包含空格,则只使用纯字符串。

I would advice you to use find_path instead of adding includes directly.我建议您使用find_path而不是直接添加包含。 There you can add PATH_SUFFIXES .在那里你可以添加PATH_SUFFIXES You get a message during configuration if the header is not found, which makes it easier to spot errors.如果未找到标头,您会在配置期间收到一条消息,这样可以更轻松地发现错误。
Documentation: https://cmake.org/cmake/help/v3.6/command/find_path.html文档: https : //cmake.org/cmake/help/v3.6/command/find_path.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM