前言
最近想使用qtmqtt集成到项目,搜索参考了网上的一些资料,里面各种问题各种坑,记录一下正确的编译过程供大家参考
一、MQTT是什么?
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布。MQTT最大优点在于,可以以极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务。作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。
二、编译步骤
1.下载源码
下载地址https://github.com/qt/qtmqtt:
此处有个大坑,千万不要直接下载源码编译,dev版本好像是基于当前qt最新版本开发,如果你当前不是安装的最新版本的qt,下载下来肯定一编译各种报错。如下切换分支下载对应版本。我这装的5.13.0版本,就下对应的版本。Qt官方的qtmqtt最低只支持到qt5.10。如果安装的是低于此版本的qt,请用另外一个版本qmqtt。https://github.com/emqtt/qmqtt
2.拷贝头文件
如果下载下来后直接编译会报如下错错:
- error: dependent ‘C:\Qt\Qt5.13.0\5.13.0\msvc2017\include\QtMqtt\qmqttglobal.h’ does not exist.
拷贝qtmqtt源码目录下的src/mqtt下面的头文件到qt的inlcude目录下,自己新建一个QtMqtt的目录,记得看清楚自己是32位还是64位,我的是32位的路径如下。完整路径:C:\Qt\Qt5.13.0\5.13.0\msvc2017\include\QtMqtt
3.集成到qt
编译完成后拷贝编译文件到qt目录集成
-
拷贝dll到qt的bin目录
Qt5Mqtt.dll Qt5Mqttd.dll —>C:\Qt\Qt5.13.0\5.13.0\msvc2017\bin -
拷贝lib prl到lib目录下
Qt5Mqtt.lib Qt5Mqttd.lib Qt5Mqtt.prl Qt5Mqttd.prl---->C:\Qt\Qt5.13.0\5.13.0\msvc2017\lib -
拷贝模块配置文件到Qt安装目录中
mkspecs\modules-inst目录下的qt_lib_mqtt.pri qt_lib_mqtt_private.pri ---->C:\Qt\Qt5.13.0\5.13.0\msvc2017\mkspecs\modules
3.使用方法
- 新建工程中引入network mqtt模块
QT += network mqtt
- 包含头文件,如果想用#include < QMqttClient > 也能使用,需要自己在qt的include的QtMqtt目录下,自己新建不带后缀的类名文件。文件中添加#include "qmqttclient.h"内容即可按#include < QMqttClient >方式直接引用。否则只能按如下方式引用。
#include "QtMqtt/qmqttclient.h"
- 官方测试地址,端口1883
broker.hivemq.com
总结
一定要下载自己的qt版本对应的版本源码,拷贝头文件到qt的include目录才能正常编译。