linux下Jsoncpp的安裝與使用

來源:互聯網
上載者:User

標籤:json   linux   jsoncpp   c++   

1.軟體準備

JsonCpp

http://sourceforge.net/projects/jsoncpp/files/

scons

http://sourceforge.net/projects/ ... /scons-2.1.0.tar.gz

說明:scons需要python,在linux中自己攜帶了python,放心使用。

2.安裝目錄為了統一安裝目錄,我建立了/hfcjm目錄放置安裝的各種檔案。/hfcjm/include放置標頭檔,/hfcjm/lib放置庫檔案

3.編譯scons

tar -zvxf scons-2.1.0.tar.gz

② 設定環境變數

export MYSCONS=解壓的路徑

export SCONS_LIB_DIR=$MYSCONS/engine

注意:這兩個環境變數至關重要。

4. 解壓jsoncpp

tar -zvxf jsoncpp-src-0.5.0.tar.gz

5. 切換到jsoncpp的解壓目錄,執行命令:切記一定要解壓切換的目錄

python $MYSCONS/script/scons platform=linux-gcc

6. 產生靜態和動態庫檔案

在jsoncpp-src-0.5.0/libs/linux-gcc-4.1.2目錄下

libjson_linux-gcc-4.4.4_libmt.a

libjson_linux-gcc-4.4.4_libmt.so

7.根據自己的用途,可以將libjson_linux-gcc-4.4.4_libmt.a和libjson_linux-gcc-4.4.1.libmt.so任意一個拷貝到/usr/lib下,我選擇了靜態庫庫檔案,將解壓後的檔案夾下的INCLUDE中的標頭檔拷貝到/usr/include/json下。

8.代碼測試:

#include <json/json.h>
#include <string>
#include <iostream>
usingnamespace std;

int main()
{
string test ="{\"id\":1,\"name\":\"kurama\"}";
Json::Reader reader;
Json::Value value;
if(reader.parse(test,value))
{
if(!value["id"].isNull())
{
cout << value["id"].asInt() << endl;
cout << value["name"].asString()<< endl;
}
}
return0;
}

9 編寫makefile

jsontest:jsontest.cpp
g++ -o jsontest jsontest.cpp  /usr/lib/libjson_linux-gcc-4.4.5_libmt.a

10 .運行

linux下Jsoncpp的安裝與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.