yaml-cpp0.6.0的安装,使用

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 :"<<str1<<" , str2 :"<<str2<<std::endl;
if ((params[str1][str2]))
{
strs = params[str1][str2].as<std::vector>();
std::cout <<“strs.size() : “<<strs.size()<<std::endl;
for(int i=0;i<strs.size();i++)
{
std::cout << “v[”<<i<<”]:”<< strs.at(i) << std::endl;
}

  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::endl;
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 ###########"<<std::endl;

return 0;

}

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

编译使用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 的可执行文件

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ubuntu上安装yaml-cpp0.6.0,你可以按照以下步骤进行操作: 1. 首先,你可以通过以下命令克隆yaml-cpp的源码: ``` git clone https://github.com/jbeder/yaml-cpp.git ``` 2. 进入源码目录并创建一个build目录: ``` cd yaml-cpp && mkdir build && cd build ``` 3. 运行cmake命令来生成构建文件: ``` cmake -DYAML_BUILD_SHARED_LIBS=on .. ``` 4. 运行make命令来编译源码: ``` make ``` 5. 使用sudo权限运行make install命令来安装yaml-cpp: ``` sudo make install ``` 6. 最后,使用sudo ldconfig命令来更新共享: ``` sudo ldconfig ``` 安装完成后,你可以在`/usr/local/include`目录下找到头文件,而文件则位于`/usr/local/lib`目录下。这样,你就成功安装yaml-cpp0.6.0版本。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [Ubuntu下yaml-cpp安装](https://blog.csdn.net/taolusi/article/details/81074103)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ubuntu下yaml-cpp安装使用](https://blog.csdn.net/qq_43278899/article/details/128474075)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值