1.安装yaml-cpp
从git 上下载yaml-cpp,并进行编译
git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir -p build
cmake -DYAML_BUILD_SHARED_LIBS=ON ..
make
make install
注意,如果没有-DYAML_BUILD_SHARED_LIBS=ON,则默认生成静态库
在执行make install可能会有一些错误,适当加入sudo 权限
2. 编写加载yaml 文件的c++程序
#include <iostream>
#include "include/yaml-cpp/yaml.h"
using namespace std;
int main(int argc,char** argv)
{
YAML::Node config = YAML::LoadFile("./config/config.yml");
cout << "name:" << config["name"].as<string>() << endl;
cout << "sex:" << config["sex"].as<string>() << endl;
cout << "age:" << config["age"].as<int>() << endl;
return 0;
}
3. 编译c++程序
g++ load_yaml.cpp -o load_yaml -std=c++11 -I/usr/local/include -L/usr/local/lib64 -lyaml-cpp
编译的时候,记得加上-I(大写字母i),-L,-l(小写字母l),否则会报很多错误
4. 执行编译生成的可执行文件
./load_yaml
此时可能会报这样一个错误:./load_yaml: error while loading shared libraries: libyaml-cpp.so.0.7: cannot open shared object file: No such file or directory
只需要将在第1步编译生成的存放与/usr/local/lib64/下的libyaml-cpp.so.0.7和libyaml-cpp.so.0.7.0拷贝到/usr/local/lib下即可,拷贝时如果是权限不够,则自己加上sudo 权限即可