详情

伤心客 Lv.8

关注
由于在编译PaddleOCR中出现YamlCpp错误,所以必要编译最新的Yaml-cpp项目测试,具体步调如下:
一、下载yaml-cpp源码编译,下载地址如下:
https://gitcode.com/gh_mirrors/ya/yaml-cpp.git
https://raw.gitcode.com/gh_mirrors/ya/yaml-cpp/archive/refs/heads/master.zip
GitCode - 全球开发者的开源社区,开源代码托管平台
二、解压缩yaml-cpp-master.zip文件到D:\yaml-cpp-master目次

三、打开Cmake软件,配置源码编译选项,生成VS项目办理方案文件。

编译选项主要包罗:
3.1设置源码目次:D:/yaml-cpp-master,设置构建目次:D:/yaml-cpp-master/build_vs2022
3.1设置编译器为:VS2022+X64
3.2不勾选BUILD_TESTING 选项
3.3勾选YAML-BUILD-SHARED_LIBS选项
依次点击【Configure】按钮、【Generate】按钮、【Open Project】按钮。 
四、在VS2022软件中打开D:\yaml-cpp-master\build_vs2022\YAML_CPP.sln办理方案文件。
4.1选择编译方式为Release+X64
4.2选择的【YAML_CPP】办理方案,鼠标右键点击【重新生成办理方案】菜单后即可生成对应的动态文件到D:\yaml-cpp-master\build_vs2022\Release目次。


4.3选择的【INSTALL】 项目,鼠标右键点击【重新生成办理方案】菜单后,编译项目乐成,安装到 Cmake中配置的路径:C:\Program Files\YAML_CPP。


五、打开QTCreator软件,新建项目testYamlCppVs2022到D:\QtCode\testYamlCppVs2022目次,拷贝对应的文件夹C:\Program Files\YAML_CPP到项目D:\QtCode\testYamlCppVs2022\YAML_CPP目次下。

修改项目.pro文件,新增库引用代码如下:
  1. #添加msvc+x64编译器对应的yaml-cpp驱动引用
  2. #https://gitcode.com/gh_mirrors/ya/yaml-cpp.git
  3. INCLUDEPATH += $$PWD/YAML_CPP/include
  4. DEPENDPATH  += $$PWD/YAML_CPP/include
  5. LIBS += -L$$PWD/YAML_CPP/lib -lyaml-cpp
修改main.cpp文件代码如下:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <QApplication>
  4. #include <QDebug>
  5. #include <QFile>
  6. #include <QDir>
  7. #include <QTime>
  8. #include <QTextCodec>
  9. #include "yaml-cpp/yaml.h"
  10. struct Vec3
  11. {
  12.     double x, y, z;
  13. };
  14. struct T_SystemConfig
  15. {
  16.     std::string version;
  17.     std::vector<int> indexes;
  18.     Vec3 start;
  19.     Vec3 end;
  20.     QMap<QString, double> map;
  21. };
  22. namespace YAML
  23. {
  24.     template <>
  25.     struct convert<Vec3>
  26.     {
  27.         //Vec3解码
  28.         static bool decode(const Node &node, Vec3 &rhs)
  29.         {
  30.             rhs.x = node["x"].as<double>();
  31.             rhs.y = node["y"].as<double>();
  32.             rhs.z = node["z"].as<double>();
  33.             return true;
  34.         }
  35.     };
  36.     //Vec3  写入
  37.     YAML::Emitter &operator<<(YAML::Emitter &out, const Vec3 &rhs)
  38.     {
  39.         out << YAML::BeginMap;
  40.         out << YAML::Key << "x";
  41.         out << YAML::Value << rhs.x;
  42.         out << YAML::Key << "y";
  43.         out << YAML::Value << rhs.y;
  44.         out << YAML::Key << "z";
  45.         out << YAML::Value << rhs.z;
  46.         out << YAML::EndMap;
  47.         return out;
  48.     }
  49. }
  50. bool saveSysConfig(const QString &path, const T_SystemConfig &config)
  51. {
  52.     try
  53.     {
  54.         QTextCodec *code = QTextCodec::codecForName("GB2312");
  55.         std::string stdpath = code->fromUnicode(path).data();
  56.         YAML::Emitter tEmitter;
  57.         tEmitter << YAML::BeginMap;   //map
  58.         //std::string
  59.         tEmitter << YAML::Key << "version";
  60.         tEmitter << YAML::Value << config.version;
  61.         //std::vector<int>
  62.         tEmitter << YAML::Key << "indexes";
  63.         tEmitter << YAML::Value << YAML::Flow << config.indexes;
  64.         //Vec3
  65.         tEmitter << YAML::Key << "start";
  66.         tEmitter << YAML::Value << config.start;
  67.         tEmitter << YAML::Key << "end";
  68.         tEmitter << YAML::Value << config.end;
  69.         //QMap<QString, QString>
  70.         tEmitter << YAML::Key << "map";
  71.         tEmitter << YAML::BeginMap;
  72.         for (QString tKey : config.map.keys())
  73.         {
  74.             tEmitter << YAML::Key << tKey.toStdString();
  75.             tEmitter << YAML::Value << config.map.value(tKey);
  76.         }
  77.         tEmitter << YAML::EndMap;
  78.         tEmitter << YAML::EndMap;
  79.         std::ofstream tFout(stdpath);
  80.         tFout << tEmitter.c_str();
  81.         tFout.close();
  82.     }
  83.     catch (YAML::Exception &e)
  84.     {
  85.         qDebug() << QString(e.what());
  86.         return false;
  87.     }
  88.     return true;
  89. }
  90. bool parseSysConfig(const QString &path, T_SystemConfig &config)
  91. {
  92.     try
  93.     {
  94.         QTextCodec *code = QTextCodec::codecForName("GB2312");
  95.         std::string stdpath = code->fromUnicode(path).data();
  96.         YAML::Node tRoot = YAML::LoadFile(stdpath);
  97.         //std::string
  98.         config.version = tRoot["version"].as<std::string>();
  99.         qDebug() << QString::fromStdString(config.version);
  100.         //std::vector<int>
  101.         YAML::Node indexesNode = tRoot["indexes"];
  102.         for (unsigned int i = 0; i < indexesNode.size(); i++)
  103.         {
  104.             config.indexes.push_back(indexesNode[i].as<int>());
  105.             qDebug() << indexesNode[i].as<int>();
  106.         }
  107.         //Vec3
  108.         config.start = tRoot["start"].as<Vec3>();
  109.         config.end = tRoot["end"].as<Vec3>();
  110.         qDebug() << config.start.x << config.start.y << config.start.z;
  111.         qDebug() << config.end.x   << config.end.y   << config.end.z;
  112.         //QMap<QString, QString>
  113.         for(YAML::const_iterator it = tRoot["map"].begin(); it != tRoot["map"].end(); ++it)
  114.         {
  115.             std::string first = it->first.as<std::string>();
  116.             double second = it->second.as<double>();
  117.             config.map.insert(QString::fromStdString(first), second);
  118.         }
  119.         qDebug()  << config.map;
  120.     }
  121.     catch (YAML::Exception &e)
  122.     {
  123.         qDebug() << QString(e.what());
  124.         return false;
  125.     }
  126.     return true;
  127. }
  128. int main(int argc, char *argv[])
  129. {
  130.     T_SystemConfig writeConfig;
  131.     writeConfig.version = "1.0.0";
  132.     writeConfig.indexes = {2, 3, 6, 7, 9, 1, 0};
  133.     writeConfig.start.x = 0;
  134.     writeConfig.start.y = 0;
  135.     writeConfig.start.z = 0;
  136.     writeConfig.end.x = 30;
  137.     writeConfig.end.y = 40;
  138.     writeConfig.end.z = 50;
  139.     writeConfig.map.insert("agr1", 22);
  140.     writeConfig.map.insert("agr2", 33);
  141.     writeConfig.map.insert("agr3", 44);
  142.     writeConfig.map.insert("agr4", 55);
  143.     saveSysConfig("test.yaml", writeConfig);
  144.     T_SystemConfig readConfig;
  145.     parseSysConfig("test.yaml", readConfig);
  146.     return 0;
  147. }
运行项目,输入正确的结果如下:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
附件: 您需要登录才可以下载或查看附件。没有账号?立即注册
36阅读
0回复

暂无评论,点我抢沙发吧