【问题标题】:Unable to include file from library Clion无法包含库 Clion 中的文件
【发布时间】:2018-03-31 03:28:38
【问题描述】:

我一直在尝试为学校项目包含现代 c++ (GitHub page) 库的 JSON。自述文件的集成部分要求用户向将使用该库的 .cpp 文件添加一行简单的代码。但是,这些行提供了一个错误,因为在包含的文件中,设计器使用#include 包含了其他文件。我正在使用使用 Cmake 的 Clion。如果没有收到以下错误消息,我目前无法运行该程序:

fatal error: nlohmann/json.hpp: No such file or directory
#include <nlohmann/json.hpp>

我的代码如下:

#include <nlohmann/json.hpp>

// for convenience
using json = nlohmann::json;

我的 Cmake:

cmake_minimum_required(VERSION 3.8)
project(assingment_5)

set(CMAKE_CXX_STANDARD 11)

include_directories( CMAKE_CURRENT_SOURCE_DIR/nlohmann)

set(SOURCE_FILES main.cpp sample Item.cpp Item.h)
add_executable(assingment_5 ${SOURCE_FILES})

nlohmann 文件夹位于项目的根目录中


更新:

感谢大家的回答。

@Justin 解决了 CMake 问题(在 cmets 中完全修复)。我知道有多种解决方法,但这是我选择的一种。

新的 CMakeLists.txt 现在看起来像:

cmake_minimum_required(VERSION 3.8)
project(assingment_5)

set(CMAKE_CXX_STANDARD 11)
set(SOURCE CMAKE_SOURCE_DIR)

#[[include_directories(${PROJECT_SOURCE_DIR})   <------ works as well]]

set(SOURCE_FILES main.cpp sample Item.cpp Item.h)
add_executable(assingment_5 ${SOURCE_FILES})

target_include_directories(assingment_5 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

【问题讨论】:

  • 如果json根目录和你的CMakeLists.txt在同一个目录下,你应该可以写add_subdirectory(nlohmann),然后在你的add_executable后面写@ 987654329@,尽管这不是最惯用的方式
  • 感谢您的快速回答!它没有用......它得到了一些与 Cmake 相关的错误:` CMakeLists.txt:7 (add_subdirectory) 处的 CMake 错误:源目录 /cygdrive/c/Users/Etienne/Documents/GitHub/Collaboration/assingment 5/nlohmann不包含 CMakeLists.txt 文件。 CMakeLists.txt:16 (target_link_libraries) 处的 CMake 错误:无法为此项目构建的目标“assignment_5”指定链接库`
  • 哦,nlohmann 目录是什么?
  • 好的。刷一下我之前说的。我现在明白你想要的是target_include_directories(assingment_5 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})(你可以像在问题中一样使用include_directories,但使用target_include_directories 是更惯用的CMake)。您原始代码中的问题有两个:1.您写了CMAKE_CURRENT_SOURCE_DIR,而您应该写${CMAKE_CURRENT_SOURCE_DIR}。 2.您在包含路径中包含nlohmann;你不想那样做
  • 确保target_include_directories 行位于 add_executable 行之后,它应该可以工作

标签: c++ json cmake clion


【解决方案1】:

为了简单,我开始使用这样的标题:

我的当前目录中有一个标题,它构成了一个简单的 CMakeLists.txt。毕竟,我只是想探索图书馆

现在您可以像这样使用所描述的using 声明并关闭并运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2016-06-03
    • 2018-08-06
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多