nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

Can't use nlohmann on Linux #4363

Closed TomyFrank closed 2 months ago

TomyFrank commented 2 months ago

Description

I've cloned the repo and tried to use it in a simple C++ app but it produces an error.

Reproduction steps

After cloning the repo, moved the json folder in my project directory then included json.hpp it got the error:

 fatal error: nlohmann/detail/json_custom_base_class.hpp: No such file or directory
   49 | #include <nlohmann/detail/json_custom_base_class.hpp>

Expected vs. actual results

I expected the string {"happy": true,"pi": 3.141} on Terminal

Minimal code example

#include <iostream>
#include <string>
#include "json/include/nlohmann/json.hpp"

  int main() {
      auto j = R"(
     {
      "happy": true,
      "pi": 3.141
     }
    )"_json;
    auto data = j.dump();
    std::cout << data << '\n';
    return 0;
}

Error messages

fatal error: nlohmann/detail/json_custom_base_class.hpp: No such file or directory
   49 | #include <nlohmann/detail/json_custom_base_class.hpp

Compiler and operating system

Linux GCC

Library version

version 3

Validation

nlohmann commented 2 months ago

You need to properly configure the library search path to the json/include folder and then use #include <nlohmann/json.hpp>.

TomyFrank commented 2 months ago

You have said it's a header-only library. so inclduing that would be enough useually but it's not. How to properly configure the library search path to the json/include?

nlohmann commented 2 months ago

You did not use the single header version which is in the single_include folder. Otherwise, follow https://github.com/nlohmann/json?tab=readme-ov-file#integration.

TomyFrank commented 2 months ago

The single_include folder doesn't exist in the repo folder so I downloaded both json and json_fwd headers in the project directory and changed the code:

#include <iostream>
#include "json.hpp"
#include "json_fwd.hpp"

  int main() {
      auto j = R"(
     {
      "happy": true,
      "pi": 3.141
     }
    )"_json;
    auto data = j.dump();
    std::cout << data << '\n';
    return 0;
}

Error:

 json.hpp:34:10: fatal error: nlohmann/adl_serializer.hpp: No such file or directory
   34 | #include <nlohmann/adl_serializer.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
nlohmann commented 2 months ago
TomyFrank commented 2 months ago

Downloaded the ZIP and inslcued json.hpp and it runs. Thanks. So the easiest way to use nlohmann-json is this way yeah?

nlohmann commented 2 months ago

Good that you managed it.