1、首先下载yaml-cpp插件:
git clone https://github.com/jbeder/yaml-cpp.git
2、编译yaml-cpp插件:
1. cd yaml-cpp 2. mkdir build 3. cd build 4. cmake .. 5. make 6. make install
安装完成后,执行:
ll usr/local/lib/libyaml-cpp.a
若找到则表明已成功安装。
3、测试例子:
test.h
#include <iostream> #include <yaml-cpp/yaml.h> template <typename T> void operator>>(const YAML::Node& node, T& i); double test_param1; double test_param2;
test.cpp
#include "test.h" template <typename T> void operator>>(const YAML::Node& node, T& i) { i = node.as<T>(); }; using namespace std; void loadYamlFile(std::string name){ YAML::Node node = YAML::LoadFile(name); node["test_param1"] >> test_param1; node["test_param2"] >> test_param2; cout << test_param1 << endl; cout << test_param2 << endl; } int main() { loadYamlFile("./test.yaml"); return 0; }
test.yaml
test_param1: 10 test_param2: 20
编译和运行代码:
g++ test.cpp -lyaml-cpp -o test ./test