1

I had too many problems with compiling different libraries for json and finally I got an already compiled library of json which is cJSON to be linked with my project but when I downloaded it, and linked it with a test c file like:

//file1.c    
 #include "cJSON.h"
    ...
    ...
    ...

then I compiled it through gcc using command:

gcc file1.c -lcJSON

it returns:

file1.c:7:19: fatal error: cJSON.h: No such file or directory
 #include "cJSON.h"
Sled
  • 18,541
  • 27
  • 119
  • 168
Zainab
  • 49
  • 2
  • 7
  • 4
    Where did you install the `cJSON.h` header file? You may need to pass `-I/dir/in/which/header/is/located` to the compiler. – fuz Nov 29 '15 at 09:54
  • @FUZxxl well, I couldn't find the "cJSON.h" header file that been created by the installation process so I pass the directory of "cJSON.h" header file that I have placed it in the home folder then I got this error: `/usr/bin/ld: cannot find -lcJSON` , since I've no big experience with linux could you explain this please?! – Zainab Nov 29 '15 at 10:38

4 Answers4

2

Well, finally after several tries I succeed to figure out the problem and fix it. since cJSON is a static library so I can not perform dynamic linking like: -lcJSON, the perfect way to compile file1.c and link cJSON lib to it, is by:

  1. Add cJSON.c and cJSON.h files to the same directory of file1.c.
  2. Include "cJSON.h" in file1.c .
  3. execute the command gcc cJSON.c file1.c -lm.

Finally compilation success.

Zainab
  • 49
  • 2
  • 7
  • 1
    Sorry, but I don't think this is a good solution and may mislead people who view it. Pulling source directly into your project means that you no longer get improvements and bug fixes. – mark sabido Jun 28 '23 at 10:06
1

The compiler doesn't know where to find the include file so you have to tell it where to look (as Fuz suggested). Assuming you installed it as a package on a linux system, it will likely be somewhere like this /usr/include/cjson/cJSON.h (to find it you can use something like find /usr -iname cjson.h). So based on where it was installed on my machine, add -I/usr/include/ to the compiler command line and use #include <cjson/cJSON.h>.

mark sabido
  • 395
  • 3
  • 8
0

the correct path to the header library is #include <cjson/cJSON.h> when installed using the default Makefile configurations.

Also, the compilation command is incorrect, the cJSON library is installed under cjson in all lowercase. Therefore, use gcc file1.c -lcjson and it will work.

As @Zainab said, the double quotes " that surround the file name are when the header file is located inside the same directory as the file you are compiling. The less/greater than symbols <> are to specify an installed library on the system.

To list installed libraries on your system (assuming you're on a *NIX system, you may run ldconfig -p). when grepping it with ldconfig -p | grep 'cJSON' nothing returns on my side, whereas grepping with cjson does return results:

        libcjson_utils.so.1 (libc6,x86-64) => /usr/lib/libcjson_utils.so.1
        libcjson_utils.so (libc6,x86-64) => /usr/lib/libcjson_utils.so
        libcjson.so.1 (libc6,x86-64) => /usr/lib/libcjson.so.1
        libcjson.so (libc6,x86-64) => /usr/lib/libcjson.so

Even more ironically, using the cjson/cjson.h header file does not work, whereas cjson/cJSON.h does indeed work.

TrevCan
  • 51
  • 1
  • 1
-1

this works for me,

#include "cJSON.h"
#include "cJSON.c"

I downloaded source code from https://github.com/DaveGamble/cJSON

I am using this code for visual studio

#define _CRT_SECURE_NO_WARNINGS 0

#include <iostream>
#include <string>
using namespace std;
#include "cJSON.h"
#include "cJSON.c"

char* cJSON_GetObjectCstr(cJSON* object, const char* name) {
    const cJSON* item = cJSON_GetObjectItem(object, name);
    return cJSON_GetStringValue(item);
}
int main() {
    cJSON* params = NULL;
    params = cJSON_CreateObject();
    cJSON_AddStringToObject(params, "id", "10");
    cout << cJSON_GetObjectCstr(params, "id") << endl;
    cJSON_Delete(params);
} 
Kamalesh P
  • 23
  • 7