当前位置 博文首页 > 小龙狗的博客:CentOS下Jsoncpp安装配置

    小龙狗的博客:CentOS下Jsoncpp安装配置

    作者:[db:作者] 时间:2021-07-09 22:01

    1. 安装

    执行命令

    [root@VM-0-9-centos ~]# cd /home
    [root@VM-0-9-centos home]# mkdir jsoncpp
    [root@VM-0-9-centos home]# cd jsoncpp/
    [root@VM-0-9-centos jsoncpp]# wget https://github.com/open-source-parsers/jsoncpp/archive/1.9.4.zip
    [root@VM-0-9-centos jsoncpp]# unzip 1.9.4.zip
    [root@VM-0-9-centos jsoncpp]# cd jsoncpp-1.9.4/
    [root@VM-0-9-centos jsoncpp-1.9.4]# cmake .
    [root@VM-0-9-centos jsoncpp-1.9.4]# make
    [root@VM-0-9-centos jsoncpp-1.9.4]# make install
    

    2. 测试

    创建测试文件夹和两个文件

    [root@VM-0-9-centos jsoncpp-1.9.4]# mkdir xltest
    [root@VM-0-9-centos jsoncpp-1.9.4]# cd xltest
    [root@VM-0-9-centos xltest]# vim jsontest.json
    [root@VM-0-9-centos xltest]# vim jsontest.cpp
    

    其中jsontest.json 如下

    [{"name":"Long", "age":6}]
    

    jsontest.cpp 如下

    #include <fstream>
    #include <iostream>
    #include <json/json.h>
    #include <cassert>
    #include <errno.h>
    #include <string.h>
    using namespace std;
    int main(void)
    {
        ifstream ifs;
        ifs.open("jsontest.json");
        assert(ifs.is_open());
        Json::Reader reader;
        Json::Value root;
        if (!reader.parse(ifs, root, false))
        {
            cout << "reader parse error: " << strerror(errno) << endl;
            return -1;
        }
        string name;
        int age;
        int size;
        size = root.size();
        cout << "total " << size << " elements" << endl;
        for (int i = 0; i < size; ++i)
        {
            name = root[i]["name"].asString();
            age = root[i]["age"].asInt();
            cout << "name: " << name << ", age: " << age << endl;
        }
        return 0;
    }
    

    编译

    [root@VM-0-9-centos xltest]# g++ jsontest2.cpp
    

    执行可执行文件看到如下,安装成功

    [root@VM-0-9-centos xltest]# ./a.out
    total 1 elements
    name: long, age: 6.
    
    

    执行可执行文件看到如下,安装成功

    3. 问题及解决

    问题如下,

    [root@VM-0-9-centos xltest]# ./a.out
    /a.out: error while loading shared libraries: libjsoncpp.so.24: cannot open shared object file: No such file or directory

    
    **解决办法**
    

    执行一下 ldconfig 就行了

    [root@VM-0-9-centos xltest]# ldconfig
    

    若出现如下提示可直接忽略,不是错误。

    ldconfig: /usr/local/lib64/libstdc++.so.6.0.28-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
    

    .
    .
    .
    .
    .
    .


    桃花仙人种桃树,又摘桃花换酒钱_

    cs