Undefined reference error while using json-c

9.5k Views Asked by At

I want to use json-c in my program. While compiling (linking) I'm getting errors:

parsejson.c:(.text.startup+0xf): undefined reference to `json_object_new_object'
parsejson.c:(.text.startup+0x1c): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x2b): undefined reference to `json_object_new_int'
parsejson.c:(.text.startup+0x3a): undefined reference to `json_object_new_boolean'
parsejson.c:(.text.startup+0x4a): undefined reference to `json_object_new_double'
parsejson.c:(.text.startup+0x52): undefined reference to `json_object_new_array'
parsejson.c:(.text.startup+0x5f): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x6e): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x7b): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x8b): undefined reference to `json_object_array_add'
parsejson.c:(.text.startup+0x96): undefined reference to `json_object_array_add'
parsejson.c:(.text.startup+0xa1): undefined reference to `json_object_array_add'
parsejson.c:(.text.startup+0xb3): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xc3): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xd3): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xe5): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xf5): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xfd): undefined reference to `json_object_to_json_string'

I've json-c and my program on the same folder and included it using #include <json-c/json.h>.

3

There are 3 best solutions below

6
On BEST ANSWER

When linking statically, gcc brings the symbols that are already encountered. So if you are passing -ljson before your source files, gcc will take the static library and then eventually does not really need anything out of it. So you should put the libraries to link against after your code.

Though you have not shared what your compilation command line say, I would recommend trying something like:

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/compiled/library parsejson.c -o parsejson -ljson
0
On

Try using

gcc parsejson.c -o parsejson -ljson-c

to compile and use

#include "json-c/json.h"

to include the header

0
On

try to use this:

#include "../json-c/json.h"

because if you user the compiler will search the json.h in standard libraries.Obviously,it isn't in the standard library.if you use what I have told you ,the compiler will search the json.h in curent workspace.