Qt6.5.2 Install the official MQTT library, use CMake to compile, Qt installs other third-party libraries general tutorial

In order to install an MQTT library in Qt, I read a lot of tutorials, many of which are older Qt versions, or the explanations are not very clear. Although the whole process is similar, in order to take care of novices (such as me) who don’t know anything, it is still necessary to write the entire process.

Table of contents

1. Install Qt

2. Download Qt official MQTT library

3. Local CMake compilation

4. Qt installs the QtMqtt module

1. bin folder

2. include folder (with subsequent operations)

3. lib folder

4. mkspecs\modules folder

5. modules folder

6. Follow-up operations on the include folder

5. Use of MQTT module

6. Supplement: Deploy Mqtt library in engineering projects

1. lib folder

2. include folder

3. Add library


1. Install Qt

There are a lot of tutorials, so I won’t go into too much detail here. If you go through the official download route, just choose the open source version. The official download website is in one step:Download Qt OSS: Get Qt Online Installer

You may need to select the components to download. I refer to the following blog and just follow the article to select the components. [Qt Installation Tutorial (Qt 6.4)_A Sheep_’s Blog-CSDN Blog]

2. Download Qt official MQTT library

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

 Clone to a local folder, preferably the file directorydoes not contain Chinese:

Then switch the git branch to the same branch as the Qt version. Here we switch to 6.5.2.

git branch -a # 查看所有分支,退出阅读按个Q键就行
git checkout remotes/origin/6.5.2  # 切换至6.5.2分支

3. Local CMake compilation

Next, we open this project using Qt. After opening Qt, click "Open Project" and when selecting the file, select CMakeLists.txt in the official library you just cloned.

Then configure the project normally.

 After opening the project, we switch to Release build mode.

 After waiting for the progress bar in the lower right corner to complete, we click the small hammer in the lower left corner to build.

 There are no errors in my build here, and the green light is all the way. In fact, there is basically no problem with downloading the official Qt normally.

4. Qt installs the QtMqtt module

After the compilation is completed, the following compilation folder appears in the directory of the same level as the code:

 We open the folder with Release suffix, then open the installation directory selected during Qt installation, and put some useful contents in Release The files are migrated to the Qt installation directory. My installation directory is: D:\Qt_code\6.5.2\mingw_64 (Don’t ask why it is installed in the folder with code, just ask with swipe)

1. bin folder

Move the Qt6Mqtt.dll file in the bin folder to the Qt bin folder

2. include folder (with subsequent operations)

Move the entire QtMqtt folder in the include folder to the Qt include folder.

Note that when we open the QtMqtt folder, we will find many header files (.h). If we open a header file at random, we will find that the content is as follows:

#include "../../../qtmqtt/src/mqtt/qmqttauthenticationproperties.h"

It is actually in the include source code,This is problematic! We will modify it later, don’t worry now.

3. lib folder

 Move the contents of the lib folder to the Qt lib folder according to the format. Specifically includes:

Folder: \lib\cmake\Qt6Mqtt → \lib\cmake

File: \lib\pkgconfig\Qt6Mqtt.pc → \lib\pkgconfig

File: \lib\libQt6Mqtt.a → \lib

File: \lib\Qt6Mqtt.prl → \lib

4. mkspecs\modules folder

Open the mkspecs folders in both directories to mkspecs\modules and move all the files inside to the Qt installation directory .

5. modules folder

Move the Mqtt.json file in the modules folder to the Qt modules folder.

6. Follow-up operations on the include folder

We just said that there is a problem with the header files in include/QtMqtt that we moved in the past! We just opened one of the header files at random, and the content is as follows:

#include "../../../qtmqtt/src/mqtt/qmqttauthenticationproperties.h"

Since it is so included, let's go to the corresponding directory to see what's going on.

 As soon as I opened it, oh, it turned out to be the official code that we had just cloned. So we replaced all the header files (.h) in the include folder just now with genuine header files. We will replace whatever header files are in the original wrong include folder. All the header files of the 6.5.2 version I am in are as follows:

qmqttauthenticationproperties.h
qmqttclient.h
qmqttconnectionproperties.h
qmqttglobal.h
qmqttmessage.h
qmqttpublishproperties.h
qmqttsubscription.h
qmqttsubscriptionproperties.h
qmqtttopicfilter.h
qmqtttopicname.h
qmqtttype.h
qtmqttexports.h
qtmqttversion.h

At this point, the installation process ends. 

5. Use of MQTT module

Official documentation:Qt MQTT 6.5.2

If you are like me and use qmake, then you only need to add QT += mqtt to .pro and you can use it. The official code we just cloned also contains some samples that we can refer to during development. The directory is: qtmqtt\examples\mqtt.

Note that when using MQTT communication, remember: do not open a proxy! Don’t open an agency! Don’t open an agency! I was troubled by this problem for a long time and couldn’t find the problem (sad)

6. Supplement: Deploy Mqtt library in engineering projects

In addition to installing the library directly in Qt once and for all, we can also deploy the Mqtt library in the project. The simple process is as follows:

1. lib folder

We open the folder of the project that needs to be deployed, and copy the entire lib folder in the Release folder generated after compilation in the third step to the project folder. Here I randomly selected a "JsonSender" project.

2. include folder

We first create a new include folder in the project folder.​ 

Then find the source code you just cloned, and find a directory like qtmqtt/src/mqtt/ (Yes, this directory is the folder we previously created in [IV. 5. include Directory found in "Follow-up Operations"), copy all header files (.h) in it to the include folder we just created.

3. Add library

Then we open the project we just created in Qt, right-click the project folder on the left, and click "Add Library"

 Here we choose an external library

Select the content we just added for the library file and include path, click Next to enter the summary, and click Finish

         

In this way, we have completed the project deployment. When using it later in this project, we just need to include it as much as we want.

Guess you like

Origin blog.csdn.net/m0_56942491/article/details/132516553