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;
}
运行项目,输入正确的结果如下: