由于在编译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文件,新增库引用
代码如下:
- #添加msvc+x64编译器对应的yaml-cpp驱动引用
- #https://gitcode.com/gh_mirrors/ya/yaml-cpp.git
- INCLUDEPATH += $$PWD/YAML_CPP/include
- DEPENDPATH += $$PWD/YAML_CPP/include
- LIBS += -L$$PWD/YAML_CPP/lib -lyaml-cpp
修改main.cpp文件
代码如下:
- #include <iostream>
- #include <fstream>
- #include <QApplication>
- #include <QDebug>
- #include <QFile>
- #include <QDir>
- #include <QTime>
- #include <QTextCodec>
- #include "yaml-cpp/yaml.h"
- struct Vec3
- {
- double x, y, z;
- };
- struct T_SystemConfig
- {
- std::string version;
- std::vector<int> indexes;
- Vec3 start;
- Vec3 end;
- QMap<QString, double> map;
- };
- namespace YAML
- {
- template <>
- struct convert<Vec3>
- {
- //Vec3解码
- static bool decode(const Node &node, Vec3 &rhs)
- {
- rhs.x = node["x"].as<double>();
- rhs.y = node["y"].as<double>();
- rhs.z = node["z"].as<double>();
- return true;
- }
- };
- //Vec3 写入
- YAML::Emitter &operator<<(YAML::Emitter &out, const Vec3 &rhs)
- {
- out << YAML::BeginMap;
- out << YAML::Key << "x";
- out << YAML::Value << rhs.x;
- out << YAML::Key << "y";
- out << YAML::Value << rhs.y;
- out << YAML::Key << "z";
- out << YAML::Value << rhs.z;
- out << YAML::EndMap;
- return out;
- }
- }
- bool saveSysConfig(const QString &path, const T_SystemConfig &config)
- {
- try
- {
- QTextCodec *code = QTextCodec::codecForName("GB2312");
- std::string stdpath = code->fromUnicode(path).data();
- YAML::Emitter tEmitter;
- tEmitter << YAML::BeginMap; //map
- //std::string
- tEmitter << YAML::Key << "version";
- tEmitter << YAML::Value << config.version;
- //std::vector<int>
- tEmitter << YAML::Key << "indexes";
- tEmitter << YAML::Value << YAML::Flow << config.indexes;
- //Vec3
- tEmitter << YAML::Key << "start";
- tEmitter << YAML::Value << config.start;
- tEmitter << YAML::Key << "end";
- tEmitter << YAML::Value << config.end;
- //QMap<QString, QString>
- tEmitter << YAML::Key << "map";
- tEmitter << YAML::BeginMap;
- for (QString tKey : config.map.keys())
- {
- tEmitter << YAML::Key << tKey.toStdString();
- tEmitter << YAML::Value << config.map.value(tKey);
- }
- tEmitter << YAML::EndMap;
- tEmitter << YAML::EndMap;
- std::ofstream tFout(stdpath);
- tFout << tEmitter.c_str();
- tFout.close();
- }
- catch (YAML::Exception &e)
- {
- qDebug() << QString(e.what());
- return false;
- }
- return true;
- }
- bool parseSysConfig(const QString &path, T_SystemConfig &config)
- {
- try
- {
- QTextCodec *code = QTextCodec::codecForName("GB2312");
- std::string stdpath = code->fromUnicode(path).data();
- YAML::Node tRoot = YAML::LoadFile(stdpath);
- //std::string
- config.version = tRoot["version"].as<std::string>();
- qDebug() << QString::fromStdString(config.version);
- //std::vector<int>
- YAML::Node indexesNode = tRoot["indexes"];
- for (unsigned int i = 0; i < indexesNode.size(); i++)
- {
- config.indexes.push_back(indexesNode[i].as<int>());
- qDebug() << indexesNode[i].as<int>();
- }
- //Vec3
- config.start = tRoot["start"].as<Vec3>();
- config.end = tRoot["end"].as<Vec3>();
- qDebug() << config.start.x << config.start.y << config.start.z;
- qDebug() << config.end.x << config.end.y << config.end.z;
- //QMap<QString, QString>
- for(YAML::const_iterator it = tRoot["map"].begin(); it != tRoot["map"].end(); ++it)
- {
- std::string first = it->first.as<std::string>();
- double second = it->second.as<double>();
- config.map.insert(QString::fromStdString(first), second);
- }
- qDebug() << config.map;
- }
- catch (YAML::Exception &e)
- {
- qDebug() << QString(e.what());
- return false;
- }
- return true;
- }
- int main(int argc, char *argv[])
- {
- T_SystemConfig writeConfig;
- writeConfig.version = "1.0.0";
- writeConfig.indexes = {2, 3, 6, 7, 9, 1, 0};
- writeConfig.start.x = 0;
- writeConfig.start.y = 0;
- writeConfig.start.z = 0;
- writeConfig.end.x = 30;
- writeConfig.end.y = 40;
- writeConfig.end.z = 50;
- writeConfig.map.insert("agr1", 22);
- writeConfig.map.insert("agr2", 33);
- writeConfig.map.insert("agr3", 44);
- writeConfig.map.insert("agr4", 55);
- saveSysConfig("test.yaml", writeConfig);
- T_SystemConfig readConfig;
- parseSysConfig("test.yaml", readConfig);
- return 0;
- }
运行项目,输入正确的结果如下:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。