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})