简体   繁体   中英

Unable to include file from library Clion

I've been trying to include the JSON for modern c++ ( GitHub page ) library for a school project. The integration part of the README requires the user to add a simple line of code to the .cpp file that will be using the library. However, the lines provide an error since within the included file, the designer included other files using the #include<>. I am using Clion which uses Cmake. I am currently unable to run the program without receiving the following error message:

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

My code goes as follows:

#include <nlohmann/json.hpp>

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

My 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})

The nlohmann folder is within the root directory of the project


UPDATE:

Thanks all for the answers.

The problem CMake problem has been solved by @Justin (Complete fix in the comments). I am aware that there are multiple ways of solving it but this is the one that I chose.

The new CMakeLists.txt now looks like:

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})

For simplicity , I started using that header like this:

I have the header within my current directory which makes for a simple CMakeLists.txt. After all, I just wanted to explore the library .

在此处输入图片说明

在此处输入图片说明

Now you can use the described using declaration like so and be off and running.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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