ARM平台移植 json-c-0.12

系统环境:Ubuntu 14.04.3 LTS
源码:json-c-0.12.tar.gz
交叉编译环境:arm-none-linux-gnueabi-

[zhaojq@virtual-machine ]# tar -xzvf  json-c-0.12 .tar.gz
[zhaojq@virtual-machine ]# cd  json-c-0.12
[zhaojq@virtual-machine ]# ./configure CC= arm-none-linux-gnueabi-gcc --host=arm-none-linux-gnueabi --build=i686-linux-gnu

手动修改config.h.in,注释掉下面两行  
//#undef malloc
//#undef realloc
不然在编译test实例时会出现undefined reference to “rpl_malloc”和undefined reference to “rpl_realloc”两处未定义错误

[zhaojq@virtual-machine ]#  libtoolize --version
[zhaojq@virtual-machine]# autoheader --version
查看通用库是否存在,不存在则安装通用库:
[zhaojq@virtual-machine ]#  apt-get install libtool
[zhaojq@virtual-machine]# apt-get install autoconf
不然在make时会出现json-c-0.12/missing: line 81: autoheader: command not found等错误

[zhaojq@virtual-machine]# sudo -i
切换到root状态下
不然在make时会出现line 1086: arm-none-linux-gnueabi-ranlib: command not found等错误

[root@virtual-machine]# make
[root@virtual-machine]# make install
安装成功

json头文件在 /usr/local/include/json目录
[zhaojq@virtual-machine /usr/local/include/json-c]# ls
arraylist.h  json_config.h  json_inttypes.h  json_object_private.h  linkhash.h
bits.h  json_c_version.h  json_object.h  json_tokener.h  printbuf.h
debug.h  json.h  json_object_iterator.h  json_util.h  random_seed.h

交叉编译后的动态库文件在 /usr/local/lib目录
[zhaojq@virtual-machine  /usr/local/lib]# ls
libjson-c.a  libjson-c.so  libjson-c.so.2.0.1
libjson-c.la  libjson-c.so.2  pkgconfig


编译test实例
[zhaojq@virtual-machine]# vim json_test.cpp
#include 
#include "json.h"

int main(int argc, char *argv[])
{
	struct json_object *infor_object = NULL;
	infor_object = json_object_new_object();
	if (NULL == infor_object)
	{
		printf("new json object failed.\n");
		return 0;
	}

	struct json_object *para_object = NULL;
	para_object = json_object_new_object();
	if (NULL == para_object)
	{
		json_object_put(infor_object);
		printf("new json object failed.\n");
		return 0;
	}

	struct json_object *array_object = NULL;
	array_object = json_object_new_array();
	if (NULL == array_object)
	{
		json_object_put(infor_object);//free
		json_object_put(para_object);//free
		printf("new json object failed.\n");
		return 0;
	}

	/*添加json值类型到数组中*/
	json_object_array_add(array_object, json_object_new_int(256));
	json_object_array_add(array_object, json_object_new_int(257));
	json_object_array_add(array_object, json_object_new_int(258));
	
	/*添加json名称和值到json对象集合中*/
	json_object_object_add(para_object, "DeviceId", json_object_new_string("sn_iso_9000"));
	json_object_object_add(para_object, "MacAddr", json_object_new_string("AA:BB:CC:DD:EE:FF"));
	json_object_object_add(para_object, "Visible", json_object_new_int(1));
	
	json_object_object_add(infor_object, "method", json_object_new_string("GetSystemInfo"));
	json_object_object_add(infor_object, "param", para_object);
	json_object_object_add(infor_object, "id", json_object_new_string("101"));
	
	/*添加数组集合到json对象中*/
	json_object_object_add(infor_object, "array", array_object);

	printf("-----------json infor ---------------------------\n");
	printf("%s\n", json_object_to_json_string(infor_object));
	printf("-----------json infor ---------------------------\n");

	struct json_object *result_object = NULL;
	json_object_object_get_ex(infor_object, "method", &result_object);
	printf("method = %s\n", json_object_to_json_string(result_object));

	json_object_object_get_ex(infor_object, "param", &result_object);
	printf("param = %s\n", json_object_to_json_string(result_object));

	json_object_object_get_ex(infor_object, "array", &result_object);
	printf("array = %s\n", json_object_to_json_string(result_object));
	int i;
	for(i = 0; i < json_object_array_length(result_object); i++)
	{
		struct json_object *obj = json_object_array_get_idx(result_object, i);
		printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
	}

	json_object_put(infor_object);//free
	return 0;
}
注:
json-c-0.12版本不能使用json_object_object_get函数,会出现如下warning
warning: 'json_object* json_object_object_get(json_object*, const char*)' is deprecated (declared at /usr/local/include/json-c/json_object.h:290)
所以使用json_object_object_get_ex函数

[zhaojq@virtual-machine]# arm-none-linux-gnueabi-g++ -ljson-c -I/usr/local/include/json-c/ -L/usr/local/lib -o json_test json_test.cpp
编译通过,在当前目录生成json_test可执行文件

将/usr/local/lib目录下得libjson.*和pkgconfig目录都拷贝到ARM设备上文件系统的/lib目录,
json_test拷贝到ARM设备上

执行./json_test
输出:
-----------json infor ---------------------------
{ "method": "GetSystemInfo", "param": { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }, "id": "101", "array": [ 256, 257, 258 ] }
-----------json infor ---------------------------
method = "GetSystemInfo"
param = { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }
array = [ 256, 257, 258 ]
     [0]=256
     [1]=257
     [2]=258
移植成功。

你可能感兴趣的:(ARM平台移植)