系統: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環境變量解決運行時找不到動態庫問題