Linux下安装libjson-c库及使用
1、从网站获取libjson-c的源码,可使用wget,链接参考后面的参考资料

2、解压:
tar xvf json-c-0.12.1.tar.gz
cd json-c-0.12.1

3、编译准备:【请务必确认系统中已安装gcc】
禁用警告错误:
sed -i s/-Werror// Makefile.in tests/Makefile.in
生成Makefile:
./configure --prefix=/usr --disable-static
这里disable static表示以动态库的形式输出,并且安装目录到/usr/下面lib。

4、执行make进行编译,如果此时提示autoheader命令not found则表示系统需要安装GNU autoconf工具:
下载好autoconf的源码:
tar xvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure
make && make install
注意,如果 autoconf命令的版本不匹配仍然会出现编译错误,此时请将当前的autoconf工具删除,重新按上面的步骤安装。
编译json-c:
make && make install
注意使用root权限。

5、检查目标安装目录是否存在libjson:
[root@TEST json-c-0.12.1]# ll /usr/lib/libjson-c.*
-rwxr-xr-x 1 root root 914 Apr 23 09:01 /usr/lib/libjson-c.la
lrwxrwxrwx 1 root root 18 Apr 23 09:01 /usr/lib/libjson-c.so -> libjson-c.so.2.0.2
lrwxrwxrwx 1 root root 18 Apr 23 09:01 /usr/lib/libjson-c.so.2 -> libjson-c.so.2.0.2
-rwxr-xr-x 1 root root 99537 Apr 23 09:01 /usr/lib/libjson-c.so.2.0.2

6、验证json-c库是否可用:
找到json-c源码目录中的tests目录,其中给出了大量测试程序,我们选择一个test_parse.c文件,进行测试:
编译测试程序:
gcc test_parse.c -I/usr/include/json-c -L/usr/lib/ -ljson-c
注意编译的参数:
-I 表示头文件的查找路径
-L表示库文件的链接路径
-l 表示要链接的库名称(不需要写lib前缀,只需要写出库名即可)
运行a.out:
[root@TEST test]# ./a.out
new_obj.to_string()="\u0003"
new_obj.to_string()="foo"
new_obj.to_string()="foo"
new_obj.to_string()="ABC"
new_obj.to_string()=null
new_obj.to_string()=NaN
new_obj.to_string()=null
new_obj.to_string()=null
new_obj.to_string()=null
new_obj.to_string()=Infinity
new_obj.to_string()=Infinity
new_obj.to_string()=-Infinity
new_obj.to_string()=-Infinity
这里仅贴出部分结果,表示我们的json库安装成功完成,测试程序正常运行!
