1

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>.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sparsh Pipley
  • 451
  • 8
  • 22
  • Show your compilation command line. Are you listing the library after `parsejson.c` or `parsejson.o`, whichever you list on the linking command line? Have you built the json-c library? Are you linking with it? – Jonathan Leffler Jun 25 '15 at 05:00
  • no i'm just using command "gcc parsejson.c", json-c is a third party lib but i've included using #include – Sparsh Pipley Jun 25 '15 at 05:55
  • The header provides declarations. It does not provide the implementations, usually. You need to link with the library, which you need to compile and perhaps install (depending on whether it's shared or static). Doesn't the README for the library tell you about this? – Jonathan Leffler Jun 25 '15 at 05:57

3 Answers3

1

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
WedaPashi
  • 3,561
  • 26
  • 42
  • 1
    You would not use `-L` with the entire path to `parsejson.c`; you might use the full path to `parsejson.c`, or you might use `-L` with a directory that contains the compiled json-c library. Or you missed a space between the metacharacter `>` and the name `parsejson.c` in your command line. – Jonathan Leffler Jun 25 '15 at 05:49
  • it's not working says bash: -L: No such file or directory – Sparsh Pipley Jun 25 '15 at 05:56
  • @Jonathan: Yes, I missed a space between `>` and `parsejson.c`. I will edit that now. Thanks. – WedaPashi Jun 25 '15 at 06:00
  • @Sparsh: I hope you are not using `<` and `>`. (You are supposed to omit that out) – WedaPashi Jun 25 '15 at 06:31
  • @WedaPashi yes i've omit < and > but still getting error – Sparsh Pipley Jun 25 '15 at 06:39
  • Suggestion: use a path such as `-L/path/to/compiled/library` rather than risk using shell metacharacters, especially important ones like `<` or `>`. – Jonathan Leffler Jun 25 '15 at 06:40
1

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.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
西蒙尼
  • 15
  • 4
0

Try using

gcc parsejson.c -o parsejson -ljson-c

to compile and use

#include "json-c/json.h"

to include the header

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30