下载安装: yams-cpp release 0.5.3, 支持 yaml 1.2标准, 比老版本语法更优雅
解压至yaml-cpp-release-0.5.3
cd yaml-cpp-release-0.5.3 mkdir build cd build cmake .. make make install
确认include文件安装到/usr/local/include中,文件夹为 yaml-cpp
创建测试文件:
main.cpp内容如下
#include <iostream> #include <fstream> #include <yaml-cpp/yaml.h> #include <time.h> int main(int argc, const char * argv[]) { // insert code here... YAML::Node config = YAML::LoadFile("config.yaml"); const std::string username = config["username"].as<std::string>(); const std::string password = config["password"].as<std::string>(); time_t ltime; time(<ime); config["lastLogin"] = ctime(<ime); std::ofstream fout("config.yaml"); fout << config; std::cout << "username: " << username <<" password " << password << std::endl; return 0; }
在main.cpp目录下创建 config.yaml, 内容为:
username: user1
password: pass1
g++ main.cpp -I/usr/local/include -L/usr/local/lib -lyaml-cpp -o test ./test
执行成功后, 查看 config.yaml,的内容为
lastLogin: "Wed Apr 27 16:48:33 2016
"
username: user1
username: user1
password: pass1
配置使用 Xcode , 使用C++进行yaml-cpp开发除要引入<yaml-cpp/yaml.h>文件外,还需要配置xcode项目配置文件:
在Xcode的build setting中
Linking
Other linker Flags 中添加 -lyaml-cpp 否则出以下错误
"YAML::LoadFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from: clang: error: linker command failed with exit code 1 (use -v to see invocation)
参考:
http://www.xuebuyuan.com/866409.html
https://github.com/jbeder/yaml-cpp/wiki/Tutorial
http://stackoverflow.com/questions/2202179/problem-using-yaml-cpp-on-os-x