yaml-cpp
以前一直以一种自定义的格式写自己程序的配置文件,非常不友好也不方便使用,最近正好想整理一下自己程序,打算使用yaml文件形式作为后续程序的配置文件,于是就上网找了下C++版本的yaml解析库,果然,开源大法好,找到了yaml-cpp,打算尝试一下。
我
的
测
试
环
境
是
W
S
L
?
U
b
u
n
t
u
20.04
我的测试环境是 WSL-Ubuntu 20.04
我的测试环境是WSL?Ubuntu20.04
安装
我下载的是0.7.0 版本
mkdir yaml && cd yaml
wget https://codeload.github.com/jbeder/yaml-cpp/tar.gz/refs/tags/yaml-cpp-0.7.0
tar -zvxf yaml-cpp-0.7.0
yaml-cpp是使用cmake管理,因此十分方便,
cd yaml-cpp-yaml-cpp-0.7.0
mkdir build && cd build
cmake [-G generator] [-YAML_BUILD_SHARED_LIBS=on|OFF] ..
make -j4
sudo make install
The generator option is the build system you’d like to use. Run cmake without arguments to see a full list of available generators.
- On Windows, you might use “Visual Studio 12 2013” (VS 2013 32-bits), or “Visual Studio 14 2015 Win64” (VS 2015 64-bits).
- On OS X, you might use “Xcode”.
- On a UNIX-like system, omit the option (for a Makefile).
yaml-cpp builds a static library by default, you may want to build a shared library by specifying -YAML_BUILD_SHARED_LIBS=ON.
这样就安装完成了
使用
接下来新建一个C++工程来尝试使用: 因为我这边平时参数设置涉及最多的其实就是数组,string,单个值,所以结合我自己使用的场景写了如下的 test.yaml :
data_name: EuRoc_data
camera_rate_hz: 20.0
image_size: [752, 480]
extri_R: [0.0148655429818, -0.999880929698, 0.00414029679422,
0.999557249008, 0.0149672133247, 0.025715529948,
-0.0257744366974, 0.00375618835797, 0.999660727178]
max_points: 256
yaml-cpp.cpp
#include "yaml-cpp/yaml.h"
#include <iostream>
#include <string>
#include <vector>
#define options_key(x) #x
int main(int argc, char const *argv[])
{
double camera_rate_hz;
int max_points;
std::string data_name;
std::vector<int> image_size;
std::vector<double> extri_R;
std::string config_path = PROJECT_PATH;
config_path.append("/config/test.yaml");
YAML::Node configures = YAML::LoadFile(config_path);
std::cout << "--------------------------------------------------------" << std::endl;
std::cout << "Yaml Node:" << std::endl;
std::cout << configures << std::endl;
std::cout << "--------------------------------------------------------" << std::endl;
camera_rate_hz = configures[options_key(camera_rate_hz)].as<decltype(camera_rate_hz)>();
max_points = configures[options_key(max_points)].as<decltype(max_points)>();
data_name = configures[options_key(data_name)].as<decltype(data_name)>();
image_size = configures[options_key(image_size)].as<decltype(image_size)>();
extri_R = configures[options_key(extri_R)].as<decltype(extri_R)>();
std::cout << "config:" << std::endl;
std::cout << "camera_rate_hz: " << camera_rate_hz << std::endl;
std::cout << "max_points: " << max_points << std::endl;
std::cout << "data_name: " << data_name << std::endl;
std::cout << "image_size: [" << image_size[0] << "," << image_size[1] <<"]"<< std::endl;
std::cout << "extri_R: " ;
for (size_t i = 0; i < 9; i++)
{
std::cout << extri_R[i] << ",";
}
std::cout << std::endl;
std::cout << "--------------------------------------------------------" << std::endl;
return 0;
}
简单写一下对应的CMakeLists.txt :
cmake_minimum_required( VERSION 3.2)
project(yaml_test)
add_definitions(-DPROJECT_PATH="${PROJECT_SOURCE_DIR}")
SET(CMAKE_CXX_COMPILE "g++")
SET(CMAKE_C_COMPILE "gcc")
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
SET( CMAKE_BUILD_TYPE "release")
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
find_package(yaml-cpp REQUIRED)
include_directories(${YAML_CPP_INCLUDE_DIR})
add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/src/yaml-test.cpp)
target_link_libraries(${PROJECT_NAME} yaml-cpp)
编译测试一下,与预期一样。 过程中,其实还有遇到一些坑,最开始写CMake文件的时候写的是target_link_libraries(${PROJECT_NAME} ${YAML_CPP_LIBRARIES}) ,link的时候一直报错undefined reference function ,后来改成 target_link_libraries(${PROJECT_NAME} yaml-cpp) 就OK了,之所以写${YAML_CPP_LIBRARIES} 是因为在yaml-cpp-config.cmake是这样写,不清楚为啥没有生效,有大佬知道的话希望可以指点一下~
结语
由于我只是使用yaml文件,所以没有深入学习yaml-cpp库本身。对于只是使用yaml作为配置使用来说暂时够用了~
|