-2

I am facing the exact same error as this but for mac os

Error message looks like this:

myfile.cpp:12:10: fatal error: json/json.h: No such file or directory

initially I was trying to run it like this:

g++-11 myfile.cpp -o myfile -ljsoncpp

then I changed it to this:

g++-11 -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I/usr/local/opt/jsoncpp/include -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I./json -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I/path/to/current/dir/json -L/usr/local/opt/jsoncpp pingpong.cpp -o pingpong -ljsoncpp

myfile.cpp was like this intitally:

#include <json/json.h>
...

then influenced by this post I changed it to this:

#include <jsoncpp/json/json.h>
...

but no matter what I do I seem to get the same error

This:

find /usr -name json.h | grep -f json/ Doesnt return anything.

PS: The path of jsoncpp that I used is what I got from this:

brew info jsoncpp

==> jsoncpp: stable 1.9.5, HEAD
Library for interacting with JSON
https://github.com/open-source-parsers/jsoncpp
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/jsoncpp.rb
License: MIT
==> Dependencies
Build: meson ✘, ninja ✘
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 4,398 (30 days), 672 (90 days), 66,483 (365 days)
install-on-request: 2,808 (30 days), 86 (90 days), 3,736 (365 days)
build-error: 0 (30 days)

brew --prefix jsoncpp

/usr/local/opt/jsoncpp
19
  • Where is the json.h file located relative to /usr/local/opt/jsoncpp/include? Commented Jun 8, 2023 at 6:26
  • @RetiredNinja which file are you asking? myfile.cpp? myfile.cpp is in my current folder. Commented Jun 8, 2023 at 6:29
  • Where is json.h located? Commented Jun 8, 2023 at 6:30
  • Here is the current directory structure: >. >├── json >│ ├── json-forwards.h >│ └── json.h >└── myfile.cpp > Commented Jun 8, 2023 at 6:33
  • What is the contents of /usr/local/opt/jsoncpp? Commented Jun 8, 2023 at 6:33

2 Answers 2

1

-I/opt/homebrew/include is the include directory for #include <json/json.h>.

-L/opt/homebrew/lib is the library directory for -ljsoncpp.

The include and the library paths are valid for the most of homebrew packages.

/opt/homebrew/opt/jsoncpp is the install directory.

You could install brew install pkg-config. Then the compiler and the linker flags can be retrieved:

$ pkg-config jsoncpp --cflags --libs
-I/opt/homebrew/Cellar/jsoncpp/1.9.5/include -L/opt/homebrew/Cellar/jsoncpp/1.9.5/lib -ljsoncpp

And the build command:

$ g++-11 myfile.cpp -o myfile `pkg-config jsoncpp --cflags --libs`
Sign up to request clarification or add additional context in comments.

2 Comments

which macos are you on, I am on osx High Siera(10.13.6). I dont have /opt/homebrew Doing a reinstall I found paths are: -I/usr/local/Cellar/jsoncpp/1.9.5/include -L/usr/local/Cellar/jsoncpp/1.9.5/lib
and pkg-config jsoncpp --cflags --libs says Package jsoncpp was not found in the pkg-config search path.
0

As @RetiredNinja pointed out converting #include <json/json.h> to #include "json/json.h" did the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.