1、从网站获取libjson-c的源码,可使用wget,链接参考后面的参考资料
2、解压:tar xvfjson-c-0.12.1.tar.gzcd 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.gzcd autoconf-2.69./configuremake && make install注意,如果 autoconf命令的版本不匹配仍然会出现编译错误,此时请将当前的autoconf工具删除,重新按上面的步骤安装。编译json-c:make && make install注意使用root权限。
5、检查目标安装目录是否存在libjson:[root@TEST json-c-0.12.1]# ll /usr/lib/libjs泠贾高框on-c.*-rwxr-xr-x 1 root root 914 Apr 23 09:01 /usr/lib/libjson-c.lalrwxrwxrwx 1 root root 18 Apr 23 09:01 /usr/lib/libjson-c.so -> libjson-c.so.2.0.2lrwxrwxrwx 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.outnew_obj.to_string()="\u0003"new_obj.to_string()="foo"new_obj.to_string()="foo"new_obj.to_string()="ABC"new_obj.to_string()=nullnew_obj.to_string()=NaNnew_obj.to_string()=nullnew_obj.to_string()=nullnew_obj.to_string()=nullnew_obj.to_string()=Infinitynew_obj.to_string()=Infinitynew_obj.to_string()=-Infinitynew_obj.to_string()=-Infinity这里仅贴出部分结果,表示我们的json库安装成功完成,测试程序正常运行!