json c库的移植和使用

 移植

[root@mfr json-c-0.8]#tar –xzvf json-c-0.8.tar.gz
[root@mfr json-c-0.8]#cd json-c-0.8
[root@mfr json-c-0.8]#./configure CC= arm-unknown-linux-gnu-gcc –host=arm-unknown-linux –build=i686-pc-linux
checking for a BSD-compatible install... /usr/bin/install -c                                                               
checking whether build environment is sane... yes                                                                          
checking for a thread-safe mkdir -p... /bin/mkdir -p                                                                       
checking for gawk... gawk                                                                                                  
checking whether make sets $(MAKE)... yes                                                                                  
checking for arm-unknown-linux-strip... no       ---------应该是 arm-unknown-linux-gnu-strip                                                                          
checking for strip... strip                                                                                                
configure: WARNING: In the future, Autoconf will not detect cross-tools                                                    
whose name does not start with the host triplet.  If you think this                                                        
configuration is useful to you, please write to [email protected].                                                          
checking for style of include used by make... GNU                                                                          
checking for arm-unknown-linux-gcc... arm-unknown-linux-gnu-gcc                                                            
checking for C compiler default output file name... a.out                                                                  
checking whether the C compiler works... yes                                                                               
checking whether we are cross compiling... yes                                                                             
checking for suffix of executables...                                                                                      
checking for suffix of object files... o                                                                                   
checking whether we are using the GNU C compiler... yes                                                                    
checking whether arm-unknown-linux-gnu-gcc accepts -g... yes                                                               
checking for arm-unknown-linux-gnu-gcc option to accept ISO C89... none needed                                             
checking dependency style of arm-unknown-linux-gnu-gcc... gcc3                                                             
checking how to run the C preprocessor... arm-unknown-linux-gnu-gcc -E                                                     
checking for grep that handles long lines and -e... /bin/grep                                                              
checking for egrep... /bin/grep -E                                                                                         
checking for ANSI C header files... yes                                                                                    
checking for sys/types.h... yes                                                                                            
checking for sys/stat.h... yes                                                                                             
checking for stdlib.h... yes                                                                                               
checking for string.h... yes                                                                                               
checking for memory.h... yes                                                                                               
checking for strings.h... yes                                                                                              
checking for inttypes.h... yes                                                                                             
checking for stdint.h... yes                                                                                               
checking for unistd.h... yes                                                                                               
checking fcntl.h usability... yes                                                                                          
checking fcntl.h presence... yes                                                                                           
checking for fcntl.h... yes                                                                                                
checking limits.h usability... yes                                                                                         
checking limits.h presence... yes                                                                                          
checking for limits.h... yes                                                                                               
checking for strings.h... (cached) yes                                                                                     
checking syslog.h usability... yes                                                                                         
checking syslog.h presence... yes                                                                                          
checking for syslog.h... yes                                                                                               
checking for unistd.h... (cached) yes                                                                                      
checking sys/param.h usability... yes                                                                                      
checking sys/param.h presence... yes                                                                                       
checking for sys/param.h... yes                                                                                            
checking stdarg.h usability... yes                                                                                         
checking stdarg.h presence... yes                                                                                          
checking for stdarg.h... yes                                                                                               
checking for an ANSI C-conforming const... yes                                                                             
checking for size_t... yes                                                                                                 
checking for vprintf... yes                                                                                                
checking for _doprnt... no                                                                                                 
checking for working memcmp... no                                                                                          
checking for stdlib.h... (cached) yes                                                                                      
checking for GNU libc compatible malloc... no                                                                              
checking for stdlib.h... (cached) yes                                                                                      
checking for GNU libc compatible realloc... no                                                                             
checking for strndup... yes                                                                                                
checking for strerror... yes                                                                                               
checking for vsnprintf... yes                                                                                              
checking for vasprintf... yes                                                                                              
checking for open... yes                                                                                                   
checking for vsyslog... yes                                                                                                
checking for strncasecmp... yes                                                                                            
checking build system type... i686-pc-linux-gnu                                                                            
checking host system type... arm-unknown-linux-gnu                                                                         
checking for a sed that does not truncate output... ./configure: line 5419: echo: write error: Broken pipe                  
/bin/sed                                                                                                                   
checking for fgrep... /bin/grep -F                                                                                         
checking for ld used by arm-unknown-linux-gnu-gcc... /usr/local/arm/arm-unknown-linux-gnu/arm-unknown-linux-gnu/bin/ld      
checking if the linker (/usr/local/arm/arm-unknown-linux-gnu/arm-unknown-linux-gnu/bin/ld) is GNU ld... yes                 
checking for BSD- or MS-compatible name lister (nm)... no                                                                  
checking for arm-unknown-linux-dumpbin... no           ---没有此命令                                                                    
checking for arm-unknown-linux-link... no        ---没有此命令                                                                            
checking for dumpbin... no                                                                                                 
checking for link... link -dump -symbols                                                                                   
checking the name lister (link -dump -symbols) interface... BSD nm                                                         
checking whether ln -s works... yes                                                                                        
checking the maximum length of command line arguments... 1966080                                                           
checking whether the shell understands some XSI constructs... yes                                                          
checking whether the shell understands " ="... yes                                                                         
checking for /usr/local/arm/arm-unknown-linux-gnu/arm-unknown-linux-gnu/bin/ld option to reload object files... -r
checking how to recognize dependent libraries... pass_all
checking for arm-unknown-linux-ar... no     ---应该是arm-unknown-linux-gnu-ar
checking for ar... ar
checking for arm-unknown-linux-strip... strip    ---应该是arm-unknown-linux-gnu-strip
checking for arm-unknown-linux-ranlib... no    ---应该是arm-unknown-linux-gnu-ranlib
checking for ranlib... ranlib
checking command to parse link -dump -symbols output from arm-unknown-linux-gnu-gcc object... failed
checking for dlfcn.h... yes
checking for objdir... .libs
checking if arm-unknown-linux-gnu-gcc supports -fno-rtti -fno-exceptions... no
checking for arm-unknown-linux-gnu-gcc option to produce PIC... -fPIC -DPIC
checking if arm-unknown-linux-gnu-gcc PIC flag -fPIC -DPIC works... yes
checking if arm-unknown-linux-gnu-gcc static flag -static works... yes
checking if arm-unknown-linux-gnu-gcc supports -c -o file.o... yes
checking if arm-unknown-linux-gnu-gcc supports -c -o file.o... (cached) yes
checking whether the arm-unknown-linux-gnu-gcc linker (/usr/local/arm/arm-unknown-linux-gnu/arm-unknown-linux-gnu/bin/ld) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating json.pc
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands

[root@mfr json-c-0.8]# make                                                                                                 
显示内容略

[root@mfr json-c-0.8]# make install
显示内容略

至此交叉编译完毕

json头文件将被安装在 /usr/local/include/json目录
交叉编译后的动态库文件被安装在 /usr/local/lib目录

编译json源码中自带的例子
[root@mfr json-c-0.8]##arm-unknown-linux-gnu-gcc –ljson –I/usr/local/include/json/ -L/usr/local/lib –o test2 test2.c –std=c99

出现undefined reference to “rpl_malloc”和undefined reference to “rpl_realloc”两处未定义错误,费了半天劲终于发现需要手动修改Makefile和config.h.in才能解决上面问题。

手动修改Makefile
1、 STRIP=arm-unknown-linux-gnu-strip
2、 AR= arm-unknown-linux-gnu-ar
3、 RANLIB= arm-unknown-linux-gnu-ranlib

手动修改config.h.in
屏蔽掉 
#undef malloc
#undef realloc

修改完上面两个文件,重新执行make和make install

编译json源码中自带的例子,交叉编译时一定要指定 -std=c99,为什么自己百度。。。
[root@mfr json-c-0.8]##arm-unknown-linux-gnu-gcc –ljson –I/usr/local/include/json/ -L/usr/local/lib –o test2 test2.c –std=c99
编译通过,此时在当前目录生成test2可执行文件


将/usr/local/lib目录下得libjson.*和pkgconfig目录都拷贝到ARM设备上得文件系统的/lib目录,
将test2拷贝到ARM设备上文件系统的/tmp目录,执行test2可得到正确结果。。。。。



至此json-c-0.8移植成功移植到arm系统,以后可以使用c语言编写的cgi返回JSON格式数据到客户端浏览器,浏览器使用javascript解析JSON数据,这样客户端浏览器和服务器端的数据传递方式清楚了。。。。


使用

1、json对象有几种类型       与常见的int ,char,double,boolean相对应。

 

 

2、json 有自己的数据类型 对于普通数据类型:int ,char,double,boolean,等 需要转换成 json自己的类型

json_object_new_string(const char *)  由char 转换成 自己的string类型 

json_object_new_string(double)  由double转换成 自己的double类型

。。。。。

3、json_object_put(json_object *) 释放json对象占据的内存

要在代码所用功能完成后释放,也可以在json对象不被下文中的其他对象处理时,释放。否则出错。

最好的方法是在把结果传递给外面的值后,所有的json对象都没用了,一起释放。

 

你可能感兴趣的:(资源_工具)