jsoncpp在Ubuntu上的安装以及编辑CMakeLists

1. jsoncpp下载

1.1 下载方式一:安装指令

Ubuntu系统下的jsoncpp的安装指令如下:

apt install  libjsoncpp-dev

不推荐这种方法,因为在一些没有安装包的第三方库的时候无法通过apt安装包来安装的,建议大家掌握第二种下载安装方式。

1.2 下载方式二:github源码安装

源码在github上面网址如下:
https://github.com/open-source-parsers/jsoncpp/releases 可能需要科学上网,下载速度时快时慢。
或者如果Ubuntu安装了git的话,可以直接命令行:

git clone https://github.com/open-source-parsers/jsoncpp/releases

没安装git先安装git

sudo apt-get install git
git -- version

提示这个就是安装成功:
在这里插入图片描述
继续在下载完成Jsoncpp后我们需要解压Jsoncpp文件,你下载哪个就解压那个版本的,我这里是1.9.5版本

tar zxvf jsoncpp-1.9.5.tar.gz

目录如下:cd进入此文件夹
在这里插入图片描述

cd jsoncpp-1.9.5.tar.gz

创建目录

mkdir build

执行cmake命令生成makefile并且make安装

cmake ..
make
make install

更新库函数

ldconfig

这样我们的Jsoncpp就安装好了在C/C++中使用一下头文件引入

#include<json/json.h>

1.3 测试

#include<json/json.h>
#include<iostream>
using namespace std;
int main(int argc, char** argv) 
{
Json::Value root;
Json::FastWriter fast;
root["sly"]= Json::Value("111");
root["mml"]= Json::Value("222");
cout<<fast.write(root)<<endl;
return 0;
}

编译

g++ test.cpp  -o test -ljsoncpp && ./test

输出

{"mml":"222", "sly":"111"}

2. CMakeList编写

include_directories(/home/ubuntu/jsoncpp-1.9.5/build/lib/)
link_directories(/home/ubuntu/jsoncpp-1.9.5/build/lib/)
# .a文件就是所需要的库,上两行就是在绝对地址上找到他
find_library(JSON_CPP libjsoncpp.a)

add_executable(test main.cpp)
target_link_libraries(test ${JSON_CPP})
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值