【问题标题】:Is there a working example of yaml-cpp for Visual Studio?是否有适用于 Visual Studio 的 yaml-cpp 的工作示例?
【发布时间】:2023-03-13 16:24:01
【问题描述】:

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

【问题讨论】:

    标签: yaml-cpp


    【解决方案1】:

    如果您使用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”作为例子。

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      相关资源
      最近更新 更多