下载安装: yams-cpp release 0.5.3, 支持 yaml 1.2标准, 比老版本语法更优雅
https://github.com/jbeder/yaml-cpp
解压至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
#include
#include <yaml-cpp/yaml.h>
#include
int main(int argc, const char * argv[]) {
// insert code here...
YAML::Node config = YAML::LoadFile("config.yaml");
const std::string username = config["username"].as();
const std::string password = config["password"].as();
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\n"
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, std::__1::allocator > 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