最近由於工作中用到了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 }