【Qt】Qt5.14.2配置yaml-cpp

本文详细介绍了如何在Win11系统中,使用Qt5.14.2和MinGW32编译器配置yaml-cpp0.7.0库。通过下载yaml-cpp源码,使用CMake配置,编译生成libyaml-cpp.a文件,并在Qt项目中引入库和头文件,实现yaml文件读取功能。
摘要由CSDN通过智能技术生成

写在前面

本文是在Qt create中配置第三方库yaml。编译器使用MinGW32(不是vs)

1、系统环境

这里仅说明我当时配置时所使用的环境

  • win11 64位
  • Qt 5.14.2
  • yaml-cpp 0.7.0
  • CMake 3.23.2
  • 编译器:MinGW32

(yaml-cpp-master好像有点问题,每次编译完都会报错建议, 建议使用0.7.0)

关于配置MinGW32请看我的另一篇文章,第2点:配置环境变量

2、下载yaml-cpp 0.7.0

从Github上下载yaml-cpp
在这里插入图片描述
下载之后解压,打开解压后的目录,在里面新建一个名叫build的文件夹。
在这里插入图片描述

3、CMake

打开camke-gui

①选择目录:

我这里的路径是:

# Where is the source code:
H:/google_download/yaml-cpp-yaml-cpp-0.7.0

# Where to build the binaries:
H:/google_download/yaml-cpp-yaml-cpp-0.7.0/build

在这里插入图片描述
!!!注意!!!
下面的路径都原本是基于yaml-cpp-master写的,现在请自行将它改成yaml-cpp-yaml-cpp-0.7.0

②Configure选项

点击configure
在这里插入图片描述

然后选择
在这里插入图片描述
在这里插入图片描述

Compilers

# C
D:/software/Qt/Qt5.14.2/Tools/mingw730_64/bin/gcc.exe

# C++
D:/software/Qt/Qt5.14.2/Tools/mingw730_64/bin/g++.exe

在这里插入图片描述
点击Finish
在这里插入图片描述
出现红色的不用管,看到Configuring done就点击Generate
在这里插入图片描述
出现Generating done就可以把cmake-gui关掉了。

4、编译

进入刚刚我们创建的build文件夹中,你会看到刚刚CMake输出的内容。
在这里插入图片描述
将鼠标放在该文件夹界面内,按住shift键,同时点击鼠标右键,点击在此处打开Powershell窗口(s),进入Windows Powershell
在这里插入图片描述
输入下面命令,按回车键执行该命令。

mingw32-make -j 8

这里的-j 8是为了让CPU进行多任务处理,可以加快编译速度。根据自己电脑的配置进行设置。
等待终端跑完。。。。。。。。。
在这里插入图片描述
你就可以在build文件夹中看到libyaml-cpp.a,这是我们后面需要用到的文件。
在这里插入图片描述
还有一个文件夹需要用到,就是在yaml-cpp-master目录下的include
在这里插入图片描述

5、用Qt测试

①新建Qt项目

在Qt Create中新建一个项目,按照正常流程新建即可,在选择kits的时候要注意选择MinGw 64
在这里插入图片描述

②移动xxx.a文件

在Qt的工程目录下新建一个lib文件夹
在这里插入图片描述
将我们刚刚生成的libyaml-cpp.a放到里面
在这里插入图片描述

③添加yaml-cpp

xxx.pro文件中
在这里插入图片描述
添加以下内容,根据你自己的实际情况修改:
INCLUDEPATH 就是我们刚刚提到的include文件夹, 就是在yaml-cpp-master目录下的include

INCLUDEPATH += H:\google_download\yaml-cpp-master\include

LIBS += $$PWD/lib/libyaml-cpp.a

在这里插入图片描述

④执行qmake

在这里插入图片描述
在这里插入图片描述

⑤测试

首先准备一个yaml文件,test.yaml内容是:

name: Hunzi
num: 123

在mainwindow.cpp中导入头文件

#include "yaml-cpp/yaml.h"
using namespace YAML;

在构造函数中写:

Node config;
try{
    config = LoadFile("H:/TempFiles/test.yaml");
} catch(BadFile &e) {
    qDebug() <<"read error!";
}
qDebug() << "name:" << QString::fromStdString(config["name"].as<std::string>()) << endl;
qDebug() << "num:" << config["num"].as<int>() << endl;

⑥检查kit

在这里插入图片描述

⑦运行

在这里插入图片描述

问题

如果kit不是我选择的MingGW 64的话,比如我这里选MSVC2017 64
在这里插入图片描述
在这里插入图片描述
此时你只要改回MingGW 64即可。

至此,Qt5.14.2配置yaml-cpp完成。

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Command line: -prefix /home/liuyh/workspace/qt5.14.2-arm -opensource -confirm-license -release -strip -shared -xplatform linux-arm-gnueabi-g++ -optimized-qmake -c++std c++11 --rpath=no -pch -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtgamepad -skip qtlocation -skip qtmacextras -skip qtnetworkauth -skip qtpurchasing -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtwayland -skip qtwebengine -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -make libs -make examples -nomake tools -nomake tests -gui -widgets -dbus-runtime --glib=no --iconv=no --pcre=qt --zlib=qt -no-openssl --freetype=qt --harfbuzz=qt -no-opengl -linuxfb --xcb=no -tslib --libpng=qt --libjpeg=qt --sqlite=qt -plugin-sql-sqlite -I/opt/tslib/include -L/opt/tslib/lib -recheck-all executing config test machineTuple + arm-linux-gnueabi-g++ -dumpmachine > sh: 1: arm-linux-gnueabi-g++: not found test config.qtbase.tests.machineTuple FAILED executing config test verifyspec + cd /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/config.tests/verifyspec && /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/bin/qmake "CONFIG -= qt debug_and_release app_bundle lib_bundle" "CONFIG += shared warn_off console single_arch" 'QMAKE_LIBDIR += /opt/tslib/lib' 'INCLUDEPATH += /opt/tslib/include' -early "CONFIG += cross_compile" /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/config.tests/verifyspec + cd /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/config.tests/verifyspec && MAKEFLAGS= /usr/bin/make clean && MAKEFLAGS= /usr/bin/make > rm -f verifyspec.o > rm -f *~ core *.core > arm-linux-gnueabi-g++ -c -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard -pipe -O2 -w -fPIC -I/home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/config.tests/verifyspec -I. -I/opt/tslib/include -I/home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/mkspecs/linux-arm-gnueabi-g++ -o verifyspec.o /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/config.tests/verifyspec/verifyspec.cpp > make:arm-linux-gnueabi-g++:命令未找到 > make: *** [Makefile:172:verifyspec.o] 错误 127
06-09
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值