Hope2Code
Hope2Code

Reputation: 109

Getting "json.hpp: No such file or directory" error despite having json.hpp in the same folder as main.cpp

I'm trying to integrate the json c++ library from nlohmann, while simply copying the 'single_include' file to the same directory as my main.cpp file. As per the integration instructions

json.hpp is the single required file in single_include/nlohmann or released here. You need to add

#include <nlohmann/json.hpp>

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

But for some reason the compiler thinks that no such file exists there, and I have no idea what I could possibly do differently to make this work.

Full error I'm getting:

main.cpp:2:10: fatal error: json.hpp: No such file or directory
 #include <json.hpp>
          ^~~~~~~~~~
compilation terminated.

(I'm guessing that since the json.hpp file is right next to the main.cpp file, I shouldn't write #include <nlohmann/json.hpp> despite it's being written like that in the integration instructions, right?)

*This is how my project in VS Code looks at the moment

Upvotes: 4

Views: 19743

Answers (4)

Brian Blank
Brian Blank

Reputation: 171

For me, I had to use this package instead:

sudo apt install nlohmann-json3-dev

Upvotes: 5

Dan
Dan

Reputation: 595

sudo cd /usr/include/ && wget https://github.com/nlohmann/json/releases/download/v3.10.5/json.hpp

Upvotes: 2

Tranks Naing
Tranks Naing

Reputation: 95

For me, it turned out I havn't installed that yet so this solved my problem.

sudo apt install nlohmann-json-dev

Upvotes: 5

Aplet123
Aplet123

Reputation: 35560

In C++, when headers are surrounded by angle brackets (<>), it searches for the headers in the include paths, which usually do not include the directory that your main.cpp file is located unless explicitly configured otherwise. However, when your headers are surrounded by double quotes, it searches the current directory, so you should include "json.hpp" instead of <json.hpp>.

Upvotes: 8

Related Questions