10

Am new to cpp but as per project requirements i need to consume rest api and parse the response. Am able to call api & capture the response but not able to parse it using JSONCPP library.

These are the steps i followed to parse json:

  1. Used this command to install libjsoncpp in ubuntu sudo apt-get install libjsoncpp-dev
  2. Downloaded json source files and copied json header files into project folder
  3. Compiling cpp using this command gcc -o test.out test.cpp -ljson

it is always giving

fatal error: json/json.h: No such file or directory
 #include <json/json.h>
                       ^
compilation terminated.

Didn't find any solution since 2 days. Tried this also. Can any one tell me where i am going wrong...

Nevin
  • 169
  • 1
  • 2
  • 7

2 Answers2

10

Since you're on Ubuntu I went to their package website and found the file list for the package you installed: http://packages.ubuntu.com/trusty/amd64/libjsoncpp-dev/filelist

The first few files make the problem clear:

/usr/include/jsoncpp/json/autolink.h
/usr/include/jsoncpp/json/config.h
/usr/include/jsoncpp/json/features.h
/usr/include/jsoncpp/json/forwards.h
/usr/include/jsoncpp/json/json.h

Since compilers usually look in /usr/include, you need to provide the rest of the path, i.e.:

#include <jsoncpp/json/json.h>

You could also have found this file on your running system after installing the package by running this command:

locate json.h

Or using the dpkg command after installing the package.

And when you link your program, you need to say -ljsoncpp, not -ljson.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'm confused, what does he have to change with the #include statement – A P Jan 16 '17 at 08:29
  • 1
    @AviParshan Instead of `#include `, it's `#include `. – rayryeng May 01 '18 at 17:44
  • Hello, I am having the same issue on mac. Although I installed the jsoncpp lib and cmake command runs fine, but make command complains about `"jsoncpp/json/json.h" not found` But my IDE shows the the file when I change the include into `json/json.h`and doesn't give any error but make command still says `"json/json.h" not found` have any idea? – Akil Demir Jan 25 '20 at 00:09
  • @AkilDemir: Search your disk for `json.h`. What's the full path to it? – John Zwinck Jan 26 '20 at 06:55
  • @JohnZwinck the full path to it `/usr/local/include/json/json.h`. – Akil Demir Jan 26 '20 at 12:34
  • @AkilDemir: Then on your system you need to say `#include ` and you may also need to compile with `-I/usr/local/include` if that's not working by default. – John Zwinck Jan 27 '20 at 01:49
  • @JohnZwinck As I have stated above, even when I do `` Vscode sees the the file there but make command still fails. I have another libraries over there and they just work fine. I have no idea what is goin on. Only thing is Vscode still says `include erros detected consider updating your include path`and nothing more. – Akil Demir Jan 27 '20 at 09:14
  • 1
    Right, you need to add `/usr/local/include` to your include path. – John Zwinck Jan 27 '20 at 09:20
  • @JohnZwinck I have tried auto costruct a new cmake environment with Vscode and I got an error with cmake command this time which is ```CMake Error at /usr/local/lib/cmake/jsoncpp/jsoncppConfig.cmake:75 (message): [cmake] The imported target "jsoncpp_lib" references the file [cmake] "/usr/local/lib/libjsoncpp.a" [cmake] but this file does not exist. ``` I had installed the jsoncpp via Homebrew. – Akil Demir Jan 27 '20 at 13:25
  • @JohnZwinck I have solved the problem by moving the project to Xcode. Thanks! – Akil Demir Jan 28 '20 at 10:48
  • NOTE: expected/usual consumption-side include prefix style syntax appears to be #include , not #include , according to e.g. [jsoncpp official source](https://sites.cs.ucsb.edu/~pconrad/github/jsoncpp/dist/doxygen/jsoncpp-api-html-0.6.0-dev/json__writer_8cpp_source.html). IOW, it is the job of the build system environment to be specifying the default include path (e.g.: /usr/local/include/jsoncpp[), and doing so by using the correct prefix (examples: /usr , /usr/local , /opt, ...), i.e. the one where the required installation can be found. – LinuxDev Jan 29 '21 at 13:55
5

another solution:

sudo apt-get install libjsoncpp-dev 
sudo ln -s /usr/include/jsoncpp/json/ /usr/include/json
peter zhang
  • 1,247
  • 1
  • 10
  • 19