最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Adding nlohmann JSON parser to C++17 program - Stack Overflow

programmeradmin7浏览0评论

I am trying to use the Nlohmann JSON parser in a C++17 program to parse GeoJSON files. The GitHub documentation says to add the header file json.hpp using these code lines:

#include <nlohmann/json.hpp>

using json = nlohmann::json;

I am building this in Ubuntu 22.04 just using this simple g++ compiler command:

g++ geojson geojson.cpp -std=c++17 ./geojson

But I get the following error:

geojson.cpp:3:10: fatal error: nlohmann/json.hpp: No such file or directory
    3 | #include <nlohmann/json.hpp>

I have tried downloading json.hpp from the repo and putting it in the include directory and then use

#include "include/json.hpp"

But that just kicks the can down the road as json.hpp has many includes from <nlohmann>.

I've tried vcpkg but that produces other errors.

I would really prefer to get it working with g++ as this is a very simple program, and according to their own GitHub this method should work fine.

What I am doing wrong?

I am trying to use the Nlohmann JSON parser in a C++17 program to parse GeoJSON files. The GitHub documentation says to add the header file json.hpp using these code lines:

#include <nlohmann/json.hpp>

using json = nlohmann::json;

I am building this in Ubuntu 22.04 just using this simple g++ compiler command:

g++ geojson geojson.cpp -std=c++17 ./geojson

But I get the following error:

geojson.cpp:3:10: fatal error: nlohmann/json.hpp: No such file or directory
    3 | #include <nlohmann/json.hpp>

I have tried downloading json.hpp from the repo and putting it in the include directory and then use

#include "include/json.hpp"

But that just kicks the can down the road as json.hpp has many includes from <nlohmann>.

I've tried vcpkg but that produces other errors.

I would really prefer to get it working with g++ as this is a very simple program, and according to their own GitHub this method should work fine.

What I am doing wrong?

Share Improve this question edited Mar 24 at 4:00 Remy Lebeau 601k36 gold badges507 silver badges851 bronze badges asked Mar 23 at 18:39 Ed FriesemaEd Friesema 214 bronze badges 3
  • You need to tell the compiler where the nlohmann directory is. -I <the directory above nlohmann> – Ted Lyngmo Commented Mar 23 at 18:46
  • 3 "json.hpp has many includes from <nlohmann>" - No, not if you use the single_include/nlohmann/json.hpp header – Ted Lyngmo Commented Mar 23 at 18:48
  • 2 g++ geojson geojson.cpp -std=c++17 ./geojson looks wrong. If geojson is the output, you should specify it with -o geojson, once. – Ted Lyngmo Commented Mar 23 at 18:50
Add a comment  | 

1 Answer 1

Reset to default 2
  1. For a single-file include package you're supposed to have the single-include header from the single_include/nlohmann directory from the repository, not the one from the include!

  2. your include path must include a folder that contains a folder called nlohmann that in turn contains that json.hpp from the said location.

At its simplest, something like this should work:

project/
    geojson.cpp
    nlohmann/
        json.hpp

Finally the compilation command surely ought to be something like

g++ -std=c++17 geojson.cpp -o geojson -Wall -Wextra

what you provided in your question would not even work.

发布评论

评论列表(0)

  1. 暂无评论