CentOS安裝jsoncpp


兩種安裝方式:

  1. 通過cmake安裝
  2. 通過scons安裝

cmake安裝見cmake安裝jsoncpp,scons安裝見下文。

 

1. 安裝scons

tar zxvf scons-2.5.0.tar.gz
export MYSCONS=/root/file/scons-2.5.0
export SCONS_LIB_DIR=$MYSCONS/engine

MYSCONS要換成你自己文件的路徑。

(我還把這兩行export添加到了 /etc/profile 文件中了。)

2. 編譯安裝jsoncpp

tar zxvf jsoncpp-1.8.0.tar.gz
cd jsoncpp-1.8.0
python $MYSCONS/script/scons platform=linux-gcc

3. 路徑設置

把源碼中的json文件夾拷貝到/usr/local/include目錄下

把生成的.a和.so文件拷貝到/usr/local/lib目錄下。

打開/etc/profile,添加以下內容:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib

保存,再來一句: source /etc/profile,修改立即生效。

安裝結束。

----------------------------

一個Demo:

#include <iostream>
#include "json/json.h"

using namespace std;

int main()
{
    Json::Value root;
    root["key1"] = "value1";
    root["key2"] = "value2";
    
    Json::Value value31;
    value31["key31"] = "value31";
    value31["key32"] = "value32";
    Json::Value value3;
    value3.append(value31);        // value3[0] = value31;
    root["key3"] = value3;
    //for (Json::Value::const_iterator it = value3.begin(); it != value3.end(); it++) {
    //    Json::Value currentValue = *it;
    //    for (Json::Value::const_iterator it = currentValue.begin(); it != currentValue.end(); it++) {
    //        cout << (*it).asString() << endl;
    //    }
    //}
    Json::Value value4;
    value4["key41"] = "value41";
    value4["key42"] = "value42";
    root["key4"] = value4;
    //for (Json::Value::const_iterator it = value4.begin(); it != value4.end(); it++) {
    //    cout << (*it).asString() << endl;
    //}
    Json::Value value5;
    value5[0] = "value51";
    value5[1] = "value52";
    value5[2] = "value53";
    root["key5"] = value5;
    //for (Json::Value::const_iterator it = value5.begin(); it != value5.end(); it++) {
    //    cout << (*it).asString() << endl;
    //}
    string str = root.toStyledString();
    cout << "Json 大小為: " << root.size() << endl;
    cout << "Json 值為: " << str << endl;

    for (Json::Value::const_iterator it = root.begin(); it != root.end(); it++) {
        string key = it.name();
        cout << "Current Json key is: " << key << endl;
        Json::Value value = *it;
        cout << "Current Json value is: " << value << endl;
        cout << "Current Json value size is: " << value.size() << endl;

        cout << endl;
    }

    return 0;
}
View Code

執行結果:

參考文檔:

CentOS源碼安裝Jsoncpp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM