【发布时间】:2023-03-13 16:24:01
【问题描述】:
我一直在寻找能够快速使用 Yaml-Cpp 的文档(示例代码和 YAML 文件)。如果有人能指点我,我更喜欢 Visual Studio ( 2019 ) 解决方案。
【问题讨论】:
标签: yaml-cpp
我一直在寻找能够快速使用 Yaml-Cpp 的文档(示例代码和 YAML 文件)。如果有人能指点我,我更喜欢 Visual Studio ( 2019 ) 解决方案。
【问题讨论】:
标签: yaml-cpp
如果您使用cmake,一个解决方案,您可以使用vcpkg
按照文档安装,然后vcpkg install yaml-cpp。
在您的CMakeLists.txt 之后,您可以添加:
find_package(yaml-cpp CONFIG REQUIRED)
### your other config here ...
### ...
target_link_libraries(main PRIVATE yaml-cpp) #main is your executable target's name
在你的“main.cpp”之后就足够了:
#pragma warning(push)
#pragma warning(disable: 4996)
#pragma warning(disable: 4251)
#pragma warning(disable: 4275)
#include <yaml-cpp/yaml.h>
#pragma warning(pop)
#include <fstream>
#include <filesystem> // this is C++17, just foe eg to save a YAML file.
namespace fs = std::filesystem;
int main() {
fs::path pf = "file.yaml"; // you can use std::string as well
std::ofstream wf(pf, std::ios::binary | std::ios::out);
if (!wf.is_open()) {
return 1;
}
int values[] = {1, 2, 3, 4};
YAML::Emitter out(wf);
out << YAML::BeginMap;
out << YAML::Key << "MyKey" << YAML::Value << YAML::BeginSeq;
for (auto& v : values) {
out << v;
}
out << YAML::EndSeq << YAML::EndMap;
wf.close();
if (!wf.good()) {
return 1;
}
return 0;
}
注意:#pragma warning(...) 用于禁用一些与警告相关的警告,因此是可选的。
这应该足以有一个“quicksart”作为例子。
【讨论】: