交叉编译QT-MQTT库

序言

前置条件:需要有交叉编译好的 arm 环境的 qt 工具,本文不涉及。

交叉编译 qt-mqtt 库在 arm 开发板上运行 mqtt 客户端连接 mqtt 服务器。

交叉编译过程

编译出来的 Qt Mqtt 库,要使用它有两种方式,

  • 一种是直接在项目中导入外部库和头文件
  • 一种是将其以模块的形式部署到 Qt 的安装目录,这里采用这种,不需要每次导入外部库

下载 qt-mqtt 源码,版本需要与 qt 版本相同,下载地址:

qt/qtmqtt: Qt Module to implement MQTT protocol version 3.1 and 3.1.1 http://mqtt.org/ (github.com)

解压得到下列文件:

1
2
3
router2@ubuntu:~/third_lib/qtmqtt-5.15$ ls
dist LICENSE.GPL3 qtmqtt.pro sync.profile
examples LICENSE.GPL3-EXCEPT src tests

复制头文件

先把 qtmqtt-5.15/src/mqtt 目录中的所有 .h 文件复制到 qt 源码的 include 目录中的 QtMqtt 目录,QtMqtt 目录是自行创建的

1
2
3
mkdir /home/router2/Third-party-library/QT/qt-5.15.13/include/QtMqtt

cp src/mqtt/*.h /home/router2/Third-party-library/QT/qt-5.15.13/include/QtMqtt

编译

使用 qmake 进行配置生成 Makefile 文件,然后进行编译生成 lib 库文件:

1
2
3
/home/router2/Third-party-library/QT/qt-5.15.13/bin/qmake 

make

文件如下:

1
2
3
4
router2@ubuntu:~/third_lib/qtmqtt-5.15$ ls
bin include LICENSE.GPL3-EXCEPT qtmqtt.pro tests
dist lib Makefile src
examples LICENSE.GPL3 mkspecs sync.profile

复制库文件

将编译生成 lib 库文件复制到 qt 源码的 lib 目录下:

1
cp -rfa lib/libQt5Mqtt.* /home/router2/Third-party-library/QT/qt-5.15.13/lib/

复制模块配置文件

mkspecs/ 下的目录都复制到 qt 源码的 mkspecs/ 目录下:

1
cp -rfa mkspecs/* /home/router2/Third-party-library/QT/qt-5.15.13/mkspecs/

打开 mkspecs/modules/qt_lib_mqtt.pri 内容如下:

1
2
3
4
5
6
7
8
QT_MODULE_BIN_BASE = /home/router2/third_lib/qtmqtt-5.15/bin
QT_MODULE_INCLUDE_BASE = /home/router2/third_lib/qtmqtt-5.15/include
QT_MODULE_LIB_BASE = /home/router2/third_lib/qtmqtt-5.15/lib
QT_MODULE_HOST_LIB_BASE = /home/router2/third_lib/qtmqtt-5.15/lib
include(/home/router2/third_lib/qtmqtt-5.15/mkspecs/modules-inst/qt_lib_mqtt.pri)
QT.mqtt.priority = 1
include(/home/router2/third_lib/qtmqtt-5.15/mkspecs/modules-inst/qt_lib_mqtt_private.pri)
QT.mqtt_private.priority = 1

这里的路径还是使用未复制前的路径,避免以后误删,这里修改一下路径为当前存放的路径:

1
2
3
4
5
6
7
8
QT_MODULE_BIN_BASE = /home/router2/Third-party-library/QT/qt-5.15.13/bin
QT_MODULE_INCLUDE_BASE = /home/router2/Third-party-library/QT/qt-5.15.13/include
QT_MODULE_LIB_BASE = /home/router2/Third-party-library/QT/qt-5.15.13/lib
QT_MODULE_HOST_LIB_BASE = /home/router2/Third-party-library/QT/qt-5.15.13/lib
include(/home/router2/Third-party-library/QT/qt-5.15.13/mkspecs/modules-inst/qt_lib_mqtt.pri)
QT.mqtt.priority = 1
include(/home/router2/Third-party-library/QT/qt-5.15.13/mkspecs/modules-inst/qt_lib_mqtt_private.pri)
QT.mqtt_private.priority = 1

之后新建工程时直接在 .pro 文件添加 mqtt 模块即可。

测试

将 mqtt 源码的 lib 库文件移植到开发板中,测试的时候由于需要连接服务器,所以开发板需要能访问网络

使用 qt mqtt 源码中自带的例子来编译运行,打开 example/mqtt/simpleclient

.pro 文件修改,将下列内容注释掉,否则无法独立运行:

1
2
target.path = $$[QT_INSTALL_EXAMPLES]/mqtt/simpleclient
INSTALLS += target

mainwindow.h 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtMqtt/qmqttclient.h>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

public slots:
void setClientPort(int p);

private slots:
void on_buttonConnect_clicked();
void on_buttonQuit_clicked();
void updateLogStateChange();

void brokerDisconnected();

void on_buttonPublish_clicked();

void on_buttonSubscribe_clicked();

private:
Ui::MainWindow *ui;
QMqttClient *m_client;
};

#endif // MAINWINDOW_H

mainwindow.cpp 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCore/QDateTime>
#include <QtMqtt/qmqttclient.h>
#include <QtWidgets/QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

m_client = new QMqttClient(this);
m_client->setHostname(ui->lineEditHost->text());
m_client->setPort(ui->spinBoxPort->value());

connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
connect(m_client, &QMqttClient::disconnected, this, &MainWindow::brokerDisconnected);

connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" Received Topic: ")
+ topic.name()
+ QLatin1String(" Message: ")
+ message
+ QLatin1Char('\n');
ui->editLog->insertPlainText(content);
});

connect(m_client, &QMqttClient::pingResponseReceived, this, [this]() {
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" PingResponse")
+ QLatin1Char('\n');
ui->editLog->insertPlainText(content);
});

connect(ui->lineEditHost, &QLineEdit::textChanged, m_client, &QMqttClient::setHostname);
connect(ui->spinBoxPort, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::setClientPort);
updateLogStateChange();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_buttonConnect_clicked()
{
if (m_client->state() == QMqttClient::Disconnected) {
ui->lineEditHost->setEnabled(false);
ui->spinBoxPort->setEnabled(false);
ui->buttonConnect->setText(tr("Disconnect"));
m_client->connectToHost();
} else {
ui->lineEditHost->setEnabled(true);
ui->spinBoxPort->setEnabled(true);
ui->buttonConnect->setText(tr("Connect"));
m_client->disconnectFromHost();
}
}

void MainWindow::on_buttonQuit_clicked()
{
QApplication::quit();
}

void MainWindow::updateLogStateChange()
{
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(": State Change")
+ QString::number(m_client->state())
+ QLatin1Char('\n');
ui->editLog->insertPlainText(content);
}

void MainWindow::brokerDisconnected()
{
ui->lineEditHost->setEnabled(true);
ui->spinBoxPort->setEnabled(true);
ui->buttonConnect->setText(tr("Connect"));
}

void MainWindow::setClientPort(int p)
{
m_client->setPort(p);
}

void MainWindow::on_buttonPublish_clicked()
{
if (m_client->publish(ui->lineEditTopic->text(), ui->lineEditMessage->text().toUtf8()) == -1)
QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not publish message"));
}

void MainWindow::on_buttonSubscribe_clicked()
{
auto subscription = m_client->subscribe(ui->lineEditTopic->text());
if (!subscription) {
QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not subscribe. Is there a valid connection?"));
return;
}
}

修改 ui 文件,先设置为如下进行测试:

image-20240722222448401

点击 Connect,点击 Subscribe,然后点击 Publish 运行:

image-20240722222858919

State Change2 表示连接成功

实现具有mqtt通信功能的qt程序在arm开发板上运行 ,硬件平台是linux arm 开发板imx6ull pro_arm linux mqtt-CSDN博客

Qt开发MQTT(一) 之Qt官方Qt MQTT-CSDN博客