yaml-cpp0.6.0的安装,使用

yaml-cpp0.6.0的安装,使用

  • yaml相关软件下载
    • cmake下载,安装
    • yaml下载,安装
  • yaml-cpp0.6.0库的简单使用
    • (1)创建xxx.yaml文件
    • (2)调用yaml-cpp的库函数事例代码
  • main.cpp
  • 编译使用yaml-cpp0.6.0库的代码
    • (1)编译工具链检测
    • (2)设置环境变量:(":" 后面的是动态库libyaml-cpp.so的路径)
    • (3)将头文件拷贝到项目路径下
    • (4)编译

yaml相关软件下载

(1)cmake-3.9.1-Linux-x86_64.tar.gz
下载地址:https://cmake.org/files/v3.9/cmake-3.9.1-Linux-x86_64.tar.gz

(2)yaml-cpp-yaml-cpp-0.6.0.zip
下载地址:https://github.com/jbeder/yaml-cpp/releases/tag/release-0.6.0
https://github.com/jbeder/yaml-cpp/tags

说明:经过多次编译测试,目前只有yaml-cpp-0.6.0编译通过,其他版本编译都会有问题
 cmake也可以使用其他版本,目前使用3.1.5,3.9.1,3.12.4都编译过了,其他的尚未试过

cmake下载,安装

1.解压
>>>tar zxvf cmake-3.9.1-Linux-x86_64.tar.gz

2.创建软链接
//移动解压出来的文件夹到/opt/cmake-3.9.1目录下,并改文件夹名称为cmake-3.9.1 
>>>mv cmake-3.9.1-Linux-x86_64 /opt/cmake-3.9.1    
>>>ln -sf /opt/cmake-3.9.1/bin/* /usr/bin/ //创建软链接

3.验证安装成功
>>>cmake --version

cmake version 3.9.1

CMake suite maintained andsupported by Kitware (kitware.com/cmake).

参考:https://blog.csdn.net/toptp2017/article/details/80246963

yaml下载,安装

1.解压
>>>sudo unzip yaml-cpp-yaml-cpp-0.6.0.zip

2.创建编译文件夹
>>>mkdir build && cd build

3.编译
若需要编译linux的库,直接编译即可
若需要编译qnx的库,需要先设置环境变量:source /opt/qnx660/qnx660-env.sh (根据配置文件路径设置)
(1)生成动态库
>>>cmake …/yaml-cpp-yaml-cpp-0.6.0 -DBUILD_SHARED_LIBS=ON
(2)生成静态库
>>>cmake …/yaml-cpp-yaml-cpp-0.6.0

编译成功后,会将生成的文件全部放在build文件夹下,其中会生成MakeFile
如下表示成功了:
root@archermind:/home/archermind/Desktop/yaml/yaml-0.6.0/build-linux# cmake …/yaml-cpp-yaml-cpp-0.6.0 -DBUILD_SHARED_LIBS=ON
CMake Deprecation Warning at CMakeLists.txt:9 (cmake_policy):
The OLD behavior for policy CMP0012 will be removed from a future version
of CMake.

The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term circumstances. Projects should be ported to the NEW
behavior and not rely on setting a policy to OLD.

CMake Deprecation Warning at CMakeLists.txt:13 (cmake_policy):
The OLD behavior for policy CMP0015 will be removed from a future version
of CMake.

The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term circumstances. Projects should be ported to the NEW
behavior and not rely on setting a policy to OLD.

– Configuring done
– Generating done
– Build files have been written to: /home/archermind/Desktop/yaml/yaml-0.6.0/build-linux

4.编译源码
>>>make

100%后会生成动态库/静态库 
如下表示成功了: 
[ 1%] Building CXX object CMakeFiles/yaml-cpp.dir/src/binary.cpp.o
[ 3%] Building CXX object CMakeFiles/yaml-cpp.dir/src/convert.cpp.o
[ 5%] Building CXX object CMakeFiles/yaml-cpp.dir/src/directives.cpp.o
[ 6%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emit.cpp.o
[ 8%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitfromevents.cpp.o
[ 10%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitter.cpp.o
......
[ 91%] Building CXX object util/CMakeFiles/parse.dir/parse.cpp.o
[ 93%] Linking CXX executable parse
[ 93%] Built target parse
[ 95%] Building CXX object util/CMakeFiles/read.dir/read.cpp.o
[ 96%] Linking CXX executable read
[ 96%] Built target read
[ 98%] Building CXX object util/CMakeFiles/sandbox.dir/sandbox.cpp.o
[100%] Linking CXX executable sandbox
[100%] Built target sandbox

查看生成的动态库(若在上面设置生成静态库,则会生成libyaml-cpp.a)
>>>ls
libyaml-cpp.so libyaml-cpp.so.0.6.0 libyaml-cpp.so.0.6 Makefile

参考:https://www.aliyun.com/jiaocheng/151276.html?spm=5176.100033.1.13.7300285e6yOwBy
   https://www.cnblogs.com/taolusi/p/9248280.html

yaml-cpp0.6.0库的简单使用

(1)创建xxx.yaml文件

新建一个文本文件,改名为 test.yaml ,请将下面"####"中间的部分,复制到 test.yaml 中
test.yaml:
################################################
TOPIC:
camera_topic: 123.321
MARKER_INFO:
marker_num: 2
marker_1: [123.456, 111.456, 0.001, 12.607, 24.669]
marker_2: wwuubbww
CAMERA_INFO:
ImageSize: [1920, 1200]
CameraMat: !tag:yaml.org,2002:opencv-matrix
rows: 3
cols: 3
dt: d
data: [1.0764860919670821e+03, 0., 9.6500880506938051e+02, 0., 1.0767893838376233e+03, 6.0947605352309836e+02, 0., 0., 1.]
DistCoeff: !tag:yaml.org,2002:opencv-matrix
rows: 1
cols: 5
dt: d
data: [-1.1043137866105249e-01, 6.6171807358680848e-02, 7.7975431524507821e-04, -1.8559733063788477e-04, -1.1911275243422776e-02]
camera_topic: 111.111
################################################

(2)调用yaml-cpp的库函数事例代码

新建一个文本文件,改名为 main.cpp,请将下面"===="中间的部分,复制到 main.cpp 中

main.cpp

#include
#include
#include
#include “yaml-cpp/yaml.h”

//加载 xx.yaml 文件
YAML::Node loadFile(std::string file_name)
{
YAML::Node config;
if(!file_name.empty())
{
config=YAML::LoadFile(file_name);
}

return config;

}

//获取指定普通节点的数据
std::string read(YAML::Node params,std::string str1,std::string str2)
{例子
YAML::Node topic = params[str1];
std::string camera_topic;
if ((topic[str2]))
{
camera_topic = topic[str2].asstd::string();
std::cout << "camera_topic: " << camera_topic << std::endl;
}

return camera_topic;

}

//获取指定数组节点的数据
std::vector readArg(YAML::Node params,std::string str1,std::string str2)
{
std::vectorstrs;
std::cout <<" str1 :"< if ((params[str1][str2]))
{
strs = params[str1][str2].as();
std::cout <<“strs.size() : “< for(int i=0;i {
std::cout << “v[”< }

  return strs;
}

}

//更新普通数据
template
void write(YAML::Node params,std::string str,pubT value)
{
params[“TOPIC”][str]=value;

std::ofstream fout("test.yaml");
fout << params;
fout.close();

}

//更新数组数据
template
void writeArg(YAML::Node params,std::string str,std::string str2,int num,argT value)
{
params[str][str2][num]=value;

std::ofstream fout("test.yaml");
fout << params;
fout.close();

}

//增加新的数据
template
void writeNew(YAML::Node params,std::string str1,std::string str2,newT value)
{
//str1为第一个节点 , str2为需要赋值的节点
//force_insert(const Key& key, const Value& value);
params[str1].force_insert(str2,value);

std::ofstream fout("test.yaml");
fout << params;
fout.close();

}

//下面注释掉的4个函数,分别是上面使用的一个例子
int main(int argc, char *argv[])
{
std::cout<<"########## Begin ##########"< std::string file_name = “test.yaml”;
YAML::Node config = loadFile(file_name);

//read(config,"TOPIC","camera_topic");

readArg(config,"MARKER_INFO","marker_1");

//write(config,"camera_topic",123.321);

//writeArg(config,"MARKER_INFO","marker_1",2,0.001);

//writeNew(config,"MARKER_INFO","marker_2","wwuubbww");

std::cout<<"########### END ###########"<

}

==================================================================================

编译使用yaml-cpp0.6.0库的代码

(1)编译工具链检测

目前楼主使用以下配置编译成功,若未安装请百度查询安装g++
>>>sudo su    //获取超级权限
>>>g++ --version //查看g++版本
root@archermind:# g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright © 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

(2)设置环境变量:(":" 后面的是动态库libyaml-cpp.so的路径)

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/archermind/Desktop/yaml-cpp-example/lib-linux

(3)将头文件拷贝到项目路径下

将源码中的yaml-cpp文件夹:/yaml-cpp-yaml-cpp-0.6.0/include/yaml-cpp 拷贝到项目目录下

(4)编译

>>>g++ main.cpp -o test -I ./ -L./ -lyaml-cpp -lm -lc -lpthread -ldl -lrt -std=c++11
编译成功后,会生产一个 test 的可执行文件

你可能感兴趣的:(工具类使用,yaml)