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

I am trying to use the Nlohmann JSON parser in a C++17 program to parse GeoJSON files.The GitHub docu

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744273246a4566204.html

相关推荐

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

    I am trying to use the Nlohmann JSON parser in a C++17 program to parse GeoJSON files.The GitHub docu

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信