0

I'm new to using external libraries and after some trouble with RapidJSON, I thought I try nlohmann's json library, which is supposed to be super easy to use.

I started a new console project in Visual Studio 2022 and copied the nlohmann directory within include folder, just like I did with RapidJSON. I then copied the example code:

#include <iomanip>
#include <iostream>

#include "nlohmann/json.hpp"

using json = nlohmann::json;

int main() {
  // a valid JSON text
  auto valid_text = R"( { "numbers": [1, 2, 3] } )";

  // an invalid JSON text
  auto invalid_text = R"( { "strings": ["extra", "comma", ] } )";

  std::cout << std::boolalpha << json::accept(valid_text) << ' '
            << json::accept(invalid_text) << '\n';
}

However, I get a bunch of errors:

Error (active)  E1696   cannot open source file "nlohmann/adl_serializer.hpp"   
Error (active)  E1696   cannot open source file "nlohmann/byte_container_with_subtype.hpp"
Error (active)  E1696   cannot open source file "nlohmann/detail/conversions/from_json.hpp" 
...

When I go to the top of the code and start #include "nlohmann/a, IntelliSense does suggest the file (adl_serializer.hpp in this case), so VS is aware of the copied files. Still, the code does not compile and the line

using json = nlohmann::json;

also gives me the error:

"E0276 name followed by '::' must be a class or namespace name"

What can I do to make this work?

Thank you in advance!

Markstar
  • 639
  • 9
  • 24
  • This looks like IntelliSense errors. What happens if you build the project? – 273K Nov 21 '22 at 20:33
  • 1
    Have you added `include` to the directories that the compiler searches? VS will not search there by default. – john Nov 21 '22 at 20:40
  • 2
    It looks like maybe you didn't download the single header version of that lib: https://github.com/nlohmann/json/tree/develop/single_include/nlohmann – 001 Nov 21 '22 at 20:45
  • @JohnnyMopp : Thank you, that was it! It's compiling now, though the errors are still there when it didn't compile before. – Markstar Nov 21 '22 at 21:03
  • What does _"though the errors are still there when it didn't compile before"_ mean? – Ted Lyngmo Nov 21 '22 at 21:06
  • @john : Alright, the errors disappeared after I set "Additional Include Directories" under `Project Settings -> C/C++ -> General`. Thank you! – Markstar Nov 21 '22 at 21:08
  • @TedLyngmo : After adding the single header version, it compiled but still showed me errors in the "Error List" pane. However, following john's advice solved that as well. Weired that this part did not give me trouble when using RapidJSON. – Markstar Nov 21 '22 at 21:10
  • If it compiles and shows errors at the same time, it sure sounds fishy. Anyway, glad it seems to have worked out. It's a very nice library. – Ted Lyngmo Nov 21 '22 at 21:12

0 Answers0