-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
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • Where is the json.h file located relative to `/usr/local/opt/jsoncpp/include`? – Retired Ninja Jun 08 '23 at 06:26
  • @RetiredNinja which file are you asking? `myfile.cpp`? `myfile.cpp` is in my current folder. – ishandutta2007 Jun 08 '23 at 06:29
  • Where is `json.h` located? – Retired Ninja Jun 08 '23 at 06:30
  • Here is the current directory structure: >. >├── json >│   ├── json-forwards.h >│   └── json.h >└── myfile.cpp > – ishandutta2007 Jun 08 '23 at 06:33
  • What is the contents of `/usr/local/opt/jsoncpp`? – Alan Birtles Jun 08 '23 at 06:33
  • @AlanBirtles you are right `/usr/local/opt/jsoncpp` is the wrong path . No such path exists. `brew --prefix jsoncpp` probably doesnt give the path of the package – ishandutta2007 Jun 08 '23 at 06:36
  • `-I/usr/local/opt/jsoncpp/include` tells the compiler to add that directory to where it looks for included files. If the file you're trying to include is somewhere under that directory then you need to use the relative path from the specified path to find it. So, figure out where the file is and add a `-I/some/path` that allows the compiler to find it. – Retired Ninja Jun 08 '23 at 06:37
  • @RetiredNinja I tried `-I./json ` as well, same error. – ishandutta2007 Jun 08 '23 at 06:40
  • Guys I still haven't found the answer, if you feel its too trivial and worth closing please point to the correct answer before doing so – ishandutta2007 Jun 08 '23 at 06:42
  • It seems you are not familiar with how C++ builds programs and the various steps. The first step is compilation, and that is where your build is failing, at that step. The issue is that the compiler does not find your header files you've specified in your source code. You specify the header by giving the *correct* path of the header files in the command-line. Since we don't have access to your system, you will have to figure out the exact path. The `-l` flag plays no role in compiling the source code -- that is used in the link step. – PaulMcKenzie Jun 08 '23 at 06:46
  • If the file is in the `json` directory relative to the file including it then `#include "json/json.h"` should find it without any `-I` on the command line. You've shown two examples for how you tried including the file so when you say "I tried -Iwhatever and it didn't work" I have no way of knowing what the `#include` line currently looks like. – Retired Ninja Jun 08 '23 at 06:46
  • @PaulMcKenzie only 3rd party header apart from system headers are in `/path/to/current /directory/json/` rest of the headers should be fine as I have run several c++ code on this system in past using `g++-11 myfile.cpp`. So there is nothing really more you can know by having access to my system than what I already told you. – ishandutta2007 Jun 08 '23 at 06:50
  • Run `find /usr -name json.h | grep -f json/` and show the found paths. – 273K Jun 08 '23 at 06:50
  • @RetiredNinja yes exactly, original documentation of the project neither recommends any `-L` or `-I` .As I said I couldn't get `g++-11 myfile.cpp -o myfile -ljsoncpp` working, so added all those as debugging effort. – ishandutta2007 Jun 08 '23 at 06:53
  • Did you try exactly what I said and use `#include "json/json.h"`? – Retired Ninja Jun 08 '23 at 06:55
  • @RetiredNinja yes converting `#include ` to `#include "json/json.h"` did the trick. Thanks. – ishandutta2007 Jun 08 '23 at 06:58
  • @273K didn't output anything. – ishandutta2007 Jun 08 '23 at 07:05
  • guys can someone tell me the way to find path of `jsoncpp` . there is nothing called `/usr/local/opt/jsoncpp` ? I cant get what ` brew --prefix jsoncpp` is supposed to output – ishandutta2007 Jun 08 '23 at 07:06
  • `find /usr -name json.h | grep -F json/` did not found, because the files are in `/opt`, you have confused me with `/usr`. – 273K Jun 08 '23 at 07:27

2 Answers2

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`
273K
  • 29,503
  • 10
  • 41
  • 64
  • 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 ``` – ishandutta2007 Jun 08 '23 at 08:44
  • and `pkg-config jsoncpp --cflags --libs` says `Package jsoncpp was not found in the pkg-config search path. ` – ishandutta2007 Jun 08 '23 at 08:45
0

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

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129