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:
[email protected]:~/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‘
******
错误2:没有加-std=99参数的错误
[email protected]:~/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