2

all.

I'm having trouble using the JSON-C library on an OpenWRT linux distribution. I made a simple program below in order to test it.

#include <json/json.h>

int main() {
    char * string = "{\"name\" : \"joys of programming\"}";
    json_object * jobj = json_tokener_parse(string);
}

I then compile it with the following command.

gcc test.c -o test -ljson-c

However, I get the following errors.

In function `main':
test.c:(.text+0x2c): undefined reference to `json_tokener_parse'
test.c:(.text+0x38): undefined reference to `json_tokener_parse'
collect2: ld returned 1 exit status

I even compiled it with the command below, but it still didn't work.

gcc -ljson-c test.c -o test

I then tried to add the -L parameter when compiling, but still no luck.

gcc test.c -o test -L/usr/lib -ljson-c

This OpenWRT distribution uses opkg as its package installer, and here's what displays when I run opkg install libjson-c.

Package libjson-c (0.11-2) installed in root is up to date.

I even ran the compile command with the -E parameter to see if the correct header is being used, and it seems like it is as I can find the method declaration json_tokener_parse. I'm not really sure where I'm going wrong. I'm about to do a manual installation from Github, but I don't really want to do that because I would have to manually install other programs as well. Does anyone have any suggestions?

Thanks.

Garrett
  • 353
  • 7
  • 14
  • What if you included `json_tokener.h`? – pushkin Aug 02 '15 at 20:57
  • @Pushkin I tried it, but this doesn't work either. Also, after using the -E parameter, the header file is being brought it correctly with the json.h header. – Garrett Aug 02 '15 at 21:03
  • Make sure that the `json_tokener_parse` symbol is defined in `/usr/lib/libjson-c.{a,so}`. You can do this by using the `nm` command tool, if it exists on your platform. – Jeremy Rodi Aug 02 '15 at 22:21
  • @JeremyRodi I ran the command on the .so file, and it says **no symbols**. I'm guessing that's my problem? lol. Does this mean my installed version is just bad? Would I have to get another version? – Garrett Aug 02 '15 at 22:40
  • `undefined reference` is a linker error, not a compiler error. There is no point in thinking that the problem is with header file. `nm` might not always work as the symbols might be stripped even though your library is defining the function. Can you google if `libjson-c` is actually defining `json_tokener_parse` . – tez Aug 03 '15 at 06:43

1 Answers1

1

I just had this problem myself and if you explicitly link the libraries it will compile and link: gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib main.c -o test1 -ljson

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424