yaml-cpp - 有 Visual Studio 的 yaml-cpp 的工作示例吗?

标签 yaml-cpp

我一直在寻找的不仅仅是文档(示例代码和 YAML 文件),以便能够快速使用 Yaml-Cpp。如果有人可以向我指出的话,我更喜欢 Visual Studio (2019) 解决方案。

最佳答案

如果您使用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”为例,这应该足够了。

关于yaml-cpp - 有 Visual Studio 的 yaml-cpp 的工作示例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60572175/

相关文章:

c++ - yaml-cpp 出现编译器错误

c++ - 如何使用 yaml-cpp 获取序列名称

c++ - 如何在 YAML 中发出以逗号分隔的列表

C++ YAML : How to edit/write to a node in a . yaml文件

linux - ros-indigo安装libyaml-cpp-dev错误

c++ - yaml-cpp 以什么顺序返回数据?

c++ - 如何在yaml cpp中保留插入顺序

c++ - 未定义的引用链接 yaml-cpp 程序与 mingw-w64 + cmake

yaml-cpp - 如何为特定的 yaml-cpp 节点设置发射样式