【发布时间】: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行之后,它应该可以工作