Windows平台使用VSCode配置Yaml-cpp
VSCode下载、安装
VSCode配置C++环境
CMake下载
可参考CSDN CMake下载
Yaml-cpp下载
源码地址YAML-CPP
- 下载后解压到自己目标文件夹下
- 新建build文件夹
- 进入到build文件夹
- 打开CMake-gui
- 点击“Configure”,选择“MigGW Makfiles”,出现“Configure done”后点击“Generate”。
- 在build文件夹下进入终端,输入命令
mingw32-make
- 在build文件夹下可看到“libyaml-cpp.a”
VSCode中配置
- 将“libyaml-cpp.a”移动到自己需要的lib位置,我个人的文件夹如下:
- 修改tasks.json文件内容
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\yaml-cpp\\include",
"G:\\Desktop\\CPP\\lib\\libyaml-cpp.a"
],
"options": {
"cwd": "D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe"
}
]
}
- 修改c_cpp_properties.json文件:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:\\yaml-cpp\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
测试
- test.yaml
# yaml测试样例
# null 或 NULL 为关键字,不能写
# 名称
# 字符串
name: conf file
# 版本
# 如按浮点,2.0会转换成2
# 如按字符串,保留原样
version: 2.0
# 布尔类,转换为1或0
need: true
# 时间
time: 2020-10-03T09:21:13
empty: nul
# 对象
# 加双引号会转义\n,即会换行
my:
name: late \n lee
name1: "late \n lee"
age: 99
# 块
text: |
hello
world!
# 数组
fruit:
- apple
- apple1
- apple2
- apple3
- apple4
- apple5
# 多级数组
multi:
sta:
- 110 210 ddd 99
- 133 135 1 2 1588 1509
- 310-410
- 333-444
- test.cpp
#include <iostream>
#include <yaml-cpp/yaml.h>
using namespace std;
int main(int argc,char** argv)
{
YAML::Node config;
// 当文件不存在或yaml格式出错时,抛异常
try {
config = YAML::LoadFile("./Yaml_test/test.yaml");
} catch (...) {
printf("error loading file, yaml file error or not exist.\n");
return 0;
}
// 获取类型
for (YAML::const_iterator it = config.begin(); it != config.end(); ++it) {
std::string key = it->first.as<std::string>();
YAML::Node value = it->second;
switch (value.Type()) {
case YAML::NodeType::Scalar:
printf("key: %s scalar\n", key.c_str());
break;
case YAML::NodeType::Sequence:
printf("key: %s Sequence\n", key.c_str());
cout << "seq: " << value << endl;
break;
case YAML::NodeType::Map:
printf("key: %s Map\n", key.c_str());
break;
case YAML::NodeType::Null:
printf("key: %s Null\n", key.c_str());
break;
case YAML::NodeType::Undefined:
printf("key: %s Undefined\n", key.c_str());
break;
// etc.
}
}
// 顶层
cout << "version:" << config["version"].as<float>() << endl;
cout << "version(str):" << config["version"].as<string>() << endl;
cout << "need:" << config["need"].as<bool>() << endl; // 输出值为1
cout << "time:" << config["time"].as<string>() << endl;
cout << "empty:" << config["empty"].as<string>() << endl;
try {
printf("sizeof array: %d\n", (int)config["fruit"].size());
//cout << "fruit1:\n" << config["fruit"] << endl; // 此处返回 Node
// 索引方式取
//for (int i = 0; i < (int)config["fruit"].size(); i++)
//{
// cout << "fruit2: " << config["fruit"][i].as<string>() << endl;
//}
// 单个取
for (auto item : config["fruit"])
{
cout << "fruit3: " << item.as<string>() << endl;
}
} catch(...) {
printf("fruit not ok.\n");
}
try {
printf("new sta: \n");
for (auto item : config["multi"]["sta"])
{
printf("%s \n", item.as<string>().c_str());
}
printf("\n");
} catch (...) {
//printf("key not exist...\n");
//return 0;
}
// 对于不存在的key,似乎只能用try
try {
cout << "bad:" << config["bad"].as<int>() << endl;
} catch (...) {
printf("key bad not exist...\n");
//return 0;
}
cout << "text:" << config["text"].as<string>() << endl;
// 有两层
printf("name: %s \nname1: %s \nage: %d\n",
config["my"]["name"].as<string>().c_str(),
config["my"]["name1"].as<string>().c_str(),
config["my"]["age"].as<int>());
return 0;
}
- F5生成exe文件,运行结果