当前位置: 首页 > ds >正文

Cmake编译yaml-cpp并在QT中测试成功操作步骤

由于在编译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::stringtEmitter << YAML::Key << "version";tEmitter << YAML::Value << config.version;//std::vector<int>tEmitter << YAML::Key << "indexes";tEmitter << YAML::Value << YAML::Flow << config.indexes;//Vec3tEmitter << 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::stringconfig.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>();}//Vec3config.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;
}

运行项目,输入正确的结果如下:

http://www.xdnf.cn/news/9319.html

相关文章:

  • AI情感陪伴在医疗领域的核心应用潜力
  • 打卡37天
  • HarmonyNext使用request.agent.download实现断点下载
  • 设计模式-开放封闭原则
  • 多相电机驱动控制学习(1)——基于双dq坐标系的六相PMSM驱动控制
  • C++23 新成员函数与字符串类型的改动
  • 算法竞赛中的基本数论
  • 实时技术对比:SSE vs WebSocket vs Long Polling
  • 分布式光伏接入引起农村电压越限,如何处理?
  • Vue中van-stepper与input值不同步问题及解决方案
  • 如何将联系人从 Android 传输到 PC(正确步骤)
  • 哈尔滨云前沿服务器托管,服务器租用
  • influxdb时序数据库
  • 如何制作全景VR图?
  • Linux基础I/O【文件理解与操作】
  • nt!MiInitializeSystemCache函数分析之PointerPte->u.List.NextEntry的由来
  • 深度解析 K8S Pod 控制器,从原理到企业实践
  • [ Qt ] | 常见控件(二): window相关
  • 字符串day7
  • 线上 VR 展会:独特魅力与显著特质
  • 新增 git submodule 子模块
  • 安全接口设计:筑牢对外接口的安全防线
  • 企业im怎么选? BeeWorks -安全的企业内部通讯软件
  • 设计模式-单一职责原则
  • (14)JVM弹性内存管理
  • 【自用资源分享】Protocol Buffers 构建脚本: 支持生成 ​C++、Go、Python、Java 的 Protobuf 和 gRPC 代码
  • Leetcode-5 好数对的数目
  • 全局事务标识符
  • SPSS跨域分类:自监督知识+软模板优化
  • Ubuntu 下搭建ESP32 ESP-IDF开发环境,并在windows下用VSCode通过SSH登录Ubuntu开发ESP32应用