一、下載QtMQTT源碼
源碼下載地址:
這里使用GitHub地址為例,拉取源碼庫到本地:
$ git clone https://github.com/qt/qtmqtt.git
切換工作目錄到源碼庫目錄,並變更分支到對應的 Qt 版本(這里以 5.14.2 為例):
$ cd qtmqtt
$ git checkout 5.14.2
注:如果不切換分支,直接編譯,會提示錯誤,編譯也不會通過
Windows 環境下請確保已安裝 perl,因為 Qt 的命令工具 syncqt.pl 需要 perl。
二、在 Linux 下編譯並安裝 QtMQTT
使用 qmake 命令生成用於構建模塊的 Makefile 文件。
hauto@ubuntu:~/qtsource/qtmqtt$ qmake
Info: creating stash file /home/dsyx/ws/qtmqtt/.qmake.stash
Info: creating cache file /home/dsyx/ws/qtmqtt/.qmake.cache
構建模塊,並將模塊安裝到 Qt 的安裝目錄中(詳情見 Makefile 文件):
hauto@ubuntu:~/qtsource/qtmqtt$ make all
# 此處省略構建輸出的信息
# ...
hauto@ubuntu:~/qtsource/qtmqtt$ make install
# 此處省略安裝輸出的信息
# ...
安裝 QtMQTT 文檔
hauto@ubuntu:~/qtsource/qtmqtt$ make docs
hauto@ubuntu:~/qtsource/qtmqtt$ make install_docs
注意:
在 Linux 下構建的文檔可能無法正常顯示(由於 qtmqtt.qch 文件有錯)。
可以在 Windows 下構建文檔然后將其 qtmqtt\doc 中的 qtmqtt.qch 文件復制到 Linux 下的 Qt 安裝目錄的文檔目錄下(如:Qt/Docs/Qt-5.15.0)。
安裝 QtMQTT 示例
hauto@ubuntu:~/qtsource/qtmqtt$ make sub-examples-install_subtargets
三、在 Windows 下編譯並安裝 QtMQTT
在 Windows 下進行編譯需要用到 MSVC。以 VS2017 為例,默認安裝下 VS2017 並不會將 MSVC 的運行環境配置到系統的 PATH 中,官方給出的建議是使用 VS2017 提供的批處理文件來自動配置 MSVC 的運行環境。這些批處理文件位於*** D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build*** 中,其中 vcvars32.bat 用於配置 32-bit MSVC 的運行環境;vcvars64.bat 用於配置 64-bit MSVC 的運行環境。
注:MinGW用戶請參考Linux
以 64-bit MSVC 為例,打開 cmd,並切換其工作目錄以執行批處理文件:
C:\>D:
D:\>cd D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build>vcvars64.bat
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v16.6.5
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
在 Windows 下,Qt 的命令工具同樣也需要配置運行環境。Qt 官方提供了批處理文件 qtenv2.bat 用於配置 Qt 命令工具的運行環境。這個批處理文件位於 Qt 安裝目錄下對應編譯器目錄的 bin 目錄下。
在 cmd 中切換工作目錄並執行 qtenv2.bat 以配置 Qt 命令工具的運行環境:
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build>cd D:\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin
D:\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin>qtenv2.bat
Setting up environment for Qt usage...
Remember to call vcvarsall.bat to complete environment setup!
將工作目錄切換到 QtMQTT 的源碼庫目錄,執行 qmake 命令生成用於構建模塊的 Makefile 文件:
D:\Qt\Qt5.14.2\5.14.2\msvc2017_64>cd F:\qtmqtt
F:\qtmqtt>qmake
Info: creating stash file C:\Users\dsyx\ws\qtmqtt\.qmake.stash
Info: creating cache file C:\Users\dsyx\ws\qtmqtt\.qmake.cache
構建模塊,並將模塊安裝到 Qt 的安裝目錄中(詳情見 Makefile 文件):
F:\qtmqtt>nmake all
:: 此處省略構建輸出的信息
:: ...
F:\qtmqtt>nmake install
:: 此處省略安裝輸出的信息
:: ...
安裝 QtMQTT 文檔
F:\qtmqtt>nmake docs
F:\qtmqtt>nmake install_docs
安裝 QtMQTT 示例
F:\qtmqtt>nmake sub-examples-install_subtargets
四、期間遇到的錯誤及解決方案
1.qmqttconnection.cpp:169: error: C2039: “errorOccurred”: 不是“QAbstractSocket”的成員
qmqttconnection.cpp 中 168行有這么一句:
connect(socket, &QAbstractSocket::errorOccurred, this, &QMqttConnection::transportError);
就是這句出了問題。查幫助文檔或者源代碼都可以發現 QAbstractSocket 沒有 errorOccurred 這個 信號。有的那個信號叫:
void error(QAbstractSocket::SocketError socketError)
所以要改寫成:
connect(socket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)> (&QAbstractSocket::error),
this, static_cast<void(QMqttConnection::*)(QAbstractSocket::SocketError)>(&QMqttConnection::transportError) );