首先安装libjson的c库
#apt-get install libjson0-dev libjson0
安装好后查看/usr/include/json下是否有头文件,有就对了!
gcc -o json-demo -g json-demo.c -std=c99 -I/usr/include/json -L/usr/lib/i386-linux-gnu/ -ljson
注意:
1、json-demo和json-demo.c參数须要在前面,不能在后面,见错误1
2、须要就爱如-std=c99,代码中须要,也能够用-std=gnu99,见错误2
3、-I是json的头文件的路径
4、-L是json库所在位置,这个路径可能依据系统不同而不同。建议用find / -name "libjson*"找到位置
5、-ljson不可省略,尽量放在最后,没有这个參数会出现错误1的现象
错误1:
jack@ubuntu:~/json-c$ gcc -I/usr/include/json -L/usr/lib/i386-linux-gnu/ -ljson -std=gnu99
-o json-demo -g json-demo.c
/tmp/ccaITRtr.o: In function `GetValByKey':
/home/jack/json-c/json-demo.c:13: undefined reference to `json_object_object_get'
/home/jack/json-c/json-demo.c:16: undefined reference to `json_object_get_type'
/home/jack/json-c/json-demo.c:20: undefined reference to `json_object_get_string'
/home/jack/json-c/json-demo.c:23: undefined reference to `json_object_get_int'
/tmp/ccaITRtr.o: In function `main':
/home/jack/json-c/json-demo.c:39: undefined reference to `json_object_new_string'
/home/jack/json-c/json-demo.c:41: undefined reference to `json_object_get_string'
/home/jack/json-c/json-demo.c:43: undefined reference to `json_object_to_json_string'
******
/tmp/ccaITRtr.o: In function `GetValByKey':
/home/jack/json-c/json-demo.c:13: undefined reference to `json_object_object_get'
/home/jack/json-c/json-demo.c:16: undefined reference to `json_object_get_type'
/home/jack/json-c/json-demo.c:20: undefined reference to `json_object_get_string'
/home/jack/json-c/json-demo.c:23: undefined reference to `json_object_get_int'
/tmp/ccaITRtr.o: In function `main':
/home/jack/json-c/json-demo.c:39: undefined reference to `json_object_new_string'
/home/jack/json-c/json-demo.c:41: undefined reference to `json_object_get_string'
/home/jack/json-c/json-demo.c:43: undefined reference to `json_object_to_json_string'
******
错误2:没有加-std=99參数的错误
jack@ubuntu:~/json-c$ gcc -o json-demo -g json-demo.c -I/usr/include/json -L/usr/lib/i386-linux-gnu/ -ljson
json-demo.c: In function ‘main’:
json-demo.c:87:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
json-demo.c:87:2: note: use option -std=c99 or -std=gnu99 to compile your code
json-demo.c: In function ‘main’:
json-demo.c:87:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
json-demo.c:87:2: note: use option -std=c99 or -std=gnu99 to compile your code