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

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;
}

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

相关文章:

  • 共现矩阵的SVD降维与低维词向量计算详解
  • 【免费】【无需登录/关注】多点矩阵计算器,计算任何坐标系转换
  • Teigha应用——解析CAD文件(DWG格式)Teigha在CAD C#二次开发中的基本应用
  • 2025-05-27 Python深度学习6——神经网络模型
  • C语言_文件操作
  • Qwen2.5-VL视觉-语言模型做图片理解调研
  • Typescript学习教程,从入门到精通,TypeScript 类型声明文件详解(15)
  • 在h5端实现录音发送功能(兼容内嵌微信小程序) recorder-core
  • 了解一下C#的SortedSet
  • MicroPython 开发ESP32应用教程 之 线程介绍及实例分析
  • LockSupport与Condition解析
  • 数据库大学实验二
  • 53、用例(Use Case)详解
  • Java网络编程性能优化
  • 六大常用查找算法对比分析
  • Mybatis使用update更新值为null时不生效问题解决
  • Python+AI Agent:解锁MCP Servers的智能潜力
  • (自用)Java学习-5.16(取消收藏,批量操作,修改密码,用户更新,上传头像)
  • 相机定屏问题分析四:【cameraserver 最大request buffer超标】后置视频模式预览定屏闪退至桌面
  • 自动驾驶规划控制教程——不确定环境下的决策规划
  • 网站在美国做的服务器/济南seo顾问
  • 专业制作结婚证/外包seo公司
  • wordpress数据互通/优化模型的推广
  • 字体 安装到wordpress/广州网站优化方案
  • 青岛专业做网站的/肇庆网络推广
  • 网站建设狼雨/推广赚佣金