json_cpp使用

系统:Ubuntu16.04
G++编译器:5.4.0
jsoncpp源码:jsoncpp-src-0.5.0

一、 编译源码,获取动态库

获取源码链接:https://sourceforge.net/projects/jsoncpp/
https://github.com/open-source-parsers/jsoncpp

# 先安装 scons
$ sudo apt-get install scons
$ scons platform=linux-gcc
目标路径:
动态库:./libs/linux-gcc-5.4.0/libjson_linux-gcc-5.4.0_libmt.so
静态库:./libs/linux-gcc-5.4.0/libjson_linux-gcc-5.4.0_libmt.a

编译arm架构下动态库如下,来自链接:https://blog.csdn.net/a_ran/article/details/45277085

arm平台编译
注:platform 没有包含 arm 平台,类似 linux-gcc,所以把源码提取出来,独立编译
解压后运行如下命令:

$ mkdir arm_jsoncpp
$ cp include/ arm_jsoncpp/ -r
$ cp src/lib_json/* arm_jsoncpp/
$ cd arm_jsoncpp/

# 编译静态库
$ arm-linux-gnueabihf-g++ -c *.cpp -I./include -fPIC
$ ar cr libjsoncpp.a *.o

# 编译动态库
$ arm-linux-gnueabihf-g++ -shared -fPIC *.cpp -I./include -o libjsoncpp.so
目标路径:
动态库:./arm_jsoncpp/libjsoncpp.so
静态库:./arm_jsoncpp/libjsoncpp.a
二、编写测试程序及Makefile

源文件:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <string>
#include <sstream>
#include "json/json.h"

using namespace std;

int main(int argc, char* argv[])
{
	float score = 99.5;
	Json::Reader reader;
    Json::Value root;
    //从文件中读取
    ifstream is;
    is.open("test_write.json", ios::binary);
    if(reader.parse(is,root)){
        Json::Value json_temp;
        json_temp["name"] = Json::Value("Tian");
        json_temp["age"] = Json::Value("18");
        json_temp["score"] = Json::Value(score);
        json_temp["interest"] = Json::Value("computer");
        root["student"].append(json_temp);
        Json::StyledWriter styled_writer;
        std::cout << styled_writer.write(root) << std::endl;
        std::ofstream ofs;
        ofs.open("test_write.json");
        ofs << styled_writer.write(root);
        ofs.close();
    }
    else{
        Json::Value root;
        Json::Value json_temp;
        json_temp["name"] = Json::Value("Tian");
        json_temp["age"] = Json::Value("18");
        json_temp["score"] = Json::Value(score);
        json_temp["interest"] = Json::Value("computer");
        root["student"].append(json_temp);
        Json::StyledWriter styled_writer;
        std::cout << styled_writer.write(root) << std::endl;
        std::ofstream ofs;
        ofs.open("test_write.json");
        ofs << styled_writer.write(root);
        ofs.close();
    }

    Json::Reader reader_test;
    Json::Value value;
    ifstream list;
    list.open("test_write.json", ios::binary);
    if(reader_test.parse(list,value))
    {
        Json::Value arrayObj = value["student"];
        std::cout << "arrayObj.size() is : " << arrayObj.size() << std::endl;
        for (unsigned int i = 0; i < arrayObj.size(); i++)
        {
            float distance = arrayObj[i]["score"].asDouble();
            std::cout << "distance is : " << distance << std::endl;
        }
        list.close();
    }
	return 0;
}

Makefile文件:

# Makefile for sample program
.PHONY			: all clean

# the program to build
NAME			:=json_study

# Build tools and flags
CXX			:= g++
LD			:= g++

OBJS        := json_study.o

CPPFLAGS    := -w --std=c++11 -I/home/muyangren/projectdir/develop/json_study/
LDFLAGS     := -L/home/muyangren/projectdir/develop/json_study \
                   -ljson_linux-gcc-5.4.0_libmt

all			: $(NAME)

$(NAME)		: $(OBJS)
	$(LD) -o $@ $^ $(CPPFLAGS) $(LDFLAGS)
	$(RM)  *.o

%.o		    : ./%.c
	$(CXX) $(CPPFLAGS) -c -o $@ $<

clean	    :
	$(RM) *.o $(NAME)

三、编译中遇到问题

很多编译器相关error:

/usr/include/x86_64-linux-gnu/sys/cdefs.h:41:20: error: missing binary operator before token "("
 # if __GNUC_PREREQ (4, 6) && !defined _LIBC

解决方法:
json库的头文件搜索路径必须选择json.h的上一级目录,不能使头文件所在目录,引用json.h的路径为加上"json/json.h"(上面的源文件及Makefile为修改后的)

四、测试结果

通过在终端中export环境变量解决运行时找不到动态库问题
在这里插入图片描述在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章