vs2019+cmake编译yaml-cpp库并使用
发布时间
阅读量:
阅读量
1.编译yaml-cpp库
在源码中新建build文件夹

打开cmd,进入build目录下,输入如下命令:-DYAML_BUILD_SHARED_LIBS=ON ..表示编译动态库
cmake .. -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=D:\yaml-cpp-master\build -DYAML_BUILD_SHARED_LIBS=ON ..
AI写代码

使用vs2019打开.sln文件
选择release/debug+x86/x64组合

2.使用yaml-cpp.lib
1.先将yaml-cpp.dll复制到软件运行目录下

2.设置 包含目录/库目录/链接器->输入
3.示例代码
#include "yaml-cpp\yaml.h"
string filespath = "D:/test/config.yml"//或.yaml
//读取yaml/yml
YAML::Node config = YAML::LoadFile(filespath);
int account = config["account"].as<int>();
//写入yaml/yml
ofstream fout(files[i]);
config["account"] = tem_money;
fout << config;
fout.close();
AI写代码
#include <iostream>
#include "include/yaml-cpp/yaml.h"
#include <fstream>
using namespace std;
int main(int argc,char** argv)
{
YAML::Node config = YAML::LoadFile("../config.yaml");
cout << "Node type " << config.Type() << endl;
cout << "skills type " << config["skills"].Type() << endl;
cout << "name:" << config["name"].as<string>() << endl;
cout << "sex:" << config["sex"].as<string>() << endl;
cout << "age:" << config["age"].as<int>() << endl;
cout << "skills c++:" << config["skills"]["c++"].as<int>() << endl;
cout << "skills java:" << config["skills"]["java"].as<int>() << endl;
cout << "skills android:" << config["skills"]["android"].as<int>() << endl;
cout << "skills python:" << config["skills"]["python"].as<int>() << endl;
for(YAML::const_iterator it= config["skills"].begin(); it != config["skills"].end();++it)
{
cout << it->first.as<string>() << ":" << it->second.as<int>() << endl;
}
YAML::Node test1 = YAML::Load("[1,2,3,4]");
cout << " Type: " << test1.Type() << endl;
YAML::Node test2 = YAML::Load("1");
cout << " Type: " << test2.Type() << endl;
YAML::Node test3 = YAML::Load("{'id':1,'degree':'senior'}");
cout << " Type: " << test3.Type() << endl;
ofstream fout("testconfig.xml");
config["score"] = 99;
fout << config;
fout.close();
return 0;
}
AI写代码
4.config.yml
name: frank
sex: male
age: 18
skills:
c++: 1
java: 1
android: 1
python: 1
AI写代码
全部评论 (0)
还没有任何评论哟~
