What am I missing in the compiler settings for linking to the JSON-C static library?

I am trying to compile json-c-0.9 test binaries statically linking to libjson.a , which I created and sit in /path/to/json-c-0.9/lib :

 $ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib -ljson test1.c -o test1 

I get numerous form errors:

 /path/to/json-c-0.9/test1.c:17: undefined reference to `json_object_new_string' /path/to/json-c-0.9/test1.c:18: undefined reference to `json_object_get_string' /path/to/json-c-0.9/test1.c:19: undefined reference to `json_object_to_json_string' /path/to/json-c-0.9/test1.c:20: undefined reference to `json_object_put' /path/to/json-c-0.9/test1.c:22: undefined reference to `json_object_new_string' etc. 

What am I missing in trying to compile test binaries? Thank you for your advice.

+6
c static-libraries json-c
source share
1 answer

With static binding, gcc only tries to fetch the desired characters based on what it has already encountered. In your case, you pass -ljson in front of the source files, so gcc introduces a static library and doesn't need anything, and then tries to create your code.

Put the libraries for the link after your code.

 $ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib test1.c -o test1 -ljson 
+9
source share

All Articles