json-c初探(一) - 下载并编译源码

最近由于工作中用到了json-c,所以想仔细了解一下json-c的实现。

最开始下载了json-c-0.15源码,发现使用cmake在ubuntu14.04下编译时出现了一系列的问题,加上本身对cmake不是很了解,因此,下载了json-c-0.13.1的源码进行阅读和实验。

首先,在github上下载了源码json-c-json-c-0.13.1-20180305.zip,解压。

编译环境:ubuntu 14.04 x64

编译过程:

$ cd json-c-json-c-0.13.1-20180305/
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install   #需要用root权限安装json-c

安装完之后,会发现/usr/local/lib目录下出现了很多json-c的lib动态库

luotao@ubuntu:~/svn/learn/json/json-c-json-c-0.13.1-20180305$ ll /usr/local/lib/libjson-c.*
-rw-r--r-- 1 root root 362616 Apr 24 18:29 /usr/local/lib/libjson-c.a
-rwxr-xr-x 1 root root    949 Apr 24 18:29 /usr/local/lib/libjson-c.la*
lrwxrwxrwx 1 root root     18 Apr 24 18:29 /usr/local/lib/libjson-c.so -> libjson-c.so.4.0.0*
lrwxrwxrwx 1 root root     18 Apr 24 18:29 /usr/local/lib/libjson-c.so.4 -> libjson-c.so.4.0.0*
-rwxr-xr-x 1 root root 220879 Apr 24 18:29 /usr/local/lib/libjson-c.so.4.0.0*

验证json-c库是否可用

$ cd tests/
# 编译测试程序
$ gcc test1.c -I/usr/local/include/json-c -L/usr/lib/ -ljson-c
# 注意编译的参数
# -I表示头文件查找路径
# -L表示库文件的连接路径
# -l表示要链接的库名称(不需要写lib前缀,只需要写出库名即可)
$ ./a.out
./a.out: error while loading shared libraries: libjson-c.so.4: cannot open shared object file: No such file or directory

运行测试程序时发现报错,这里运行程序找不到json-c的动态链接库程序。解决办法:

在/etc/ld.so.conf文件中加入/usr/local/lib这一行,然后执行ldconfig -v命令生效。

luotao@ubuntu:~/svn/learn/json/json-c-json-c-0.13.1-20180305/tests$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
/usr/local/lib

luotao@ubuntu:~/svn/learn/json/json-c-json-c-0.13.1-20180305/tests$ sudo ldconfig -v

然后执行./a.out,可以看到下面输出:

luotao@ubuntu:~/svn/learn/json/json-c-json-c-0.13.1-20180305/tests$ ./a.out 
my_string=	
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_array=
	[0]=1
	[1]=2
	[2]=3
	[3]=null
	[4]=5
my_array.to_string()=[ 1, 2, 3, null, 5 ]
my_array=
	[0]=1
	[1]=2
	[2]=3
	[3]=4
	[4]=5
	[5]=null
	[6]=7
my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 2, 3, 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 3, 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ ]
after del_idx(0,1)=-1, my_array.to_string()=[ ]
after del_idx(0,7)=0, my_array.to_string()=[ ]
after del_idx(0,8)=-1, my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
after del_idx(0,6)=0, my_array.to_string()=[ 7 ]
after adding more entries, my_array.to_string()=[ 7, "s1", "s2", "s3" ]
my_array=
	[0]=3
	[1]=1
	[2]=2
	[3]=null
	[4]=0
my_array.to_string()=[ 3, 1, 2, null, 0 ]
my_array=
	[0]=null
	[1]=0
	[2]=1
	[3]=2
	[4]=3
my_array.to_string()=[ null, 0, 1, 2, 3 ]
baz_obj.to_string()="fark"
my_object=
	abc: 12
	foo: "bar"
	bool0: false
	bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
posted @ 2021-04-24 20:12  hustluotao  阅读(1506)  评论(0编辑  收藏  举报