c++macosjsoncpp

Fatal error with jsoncpp while compiling for Darwin


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

Solution

  • -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`