Integrate Paho MQTT C library into project. Updated CMakeLists.txt to find PahoMqttC package and added fallback for manual linking. Refactored mqtt.hpp and mqtt.cpp to utilize Paho MQTT C API, replacing previous async client implementation. Added Windows setup guide for MQTT library installation.
This commit is contained in:
85
MQTT_SETUP_WINDOWS.md
Normal file
85
MQTT_SETUP_WINDOWS.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# Windows MQTT库安装指南
|
||||
|
||||
## 方法1: 使用vcpkg (推荐)
|
||||
|
||||
### 1. 安装vcpkg
|
||||
```cmd
|
||||
git clone https://github.com/Microsoft/vcpkg.git
|
||||
cd vcpkg
|
||||
.\bootstrap-vcpkg.bat
|
||||
```
|
||||
|
||||
### 2. 安装MQTT库
|
||||
```cmd
|
||||
.\vcpkg install paho-mqtt:x64-windows
|
||||
.\vcpkg install paho-mqttpp3:x64-windows
|
||||
.\vcpkg install nlohmann-json:x64-windows
|
||||
```
|
||||
|
||||
### 3. 集成到CMake
|
||||
```cmd
|
||||
.\vcpkg integrate install
|
||||
```
|
||||
|
||||
### 4. 修改CMake命令
|
||||
在构建时添加vcpkg工具链:
|
||||
```cmd
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DCMAKE_TOOLCHAIN_FILE=[vcpkg根目录]/scripts/buildsystems/vcpkg.cmake
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
## 方法2: 手动编译
|
||||
|
||||
### 1. 编译Paho MQTT C库
|
||||
```cmd
|
||||
git clone https://github.com/eclipse/paho.mqtt.c.git
|
||||
cd paho.mqtt.c
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=FALSE
|
||||
cmake --build . --config Release
|
||||
cmake --install . --prefix C:/mqtt/c
|
||||
```
|
||||
|
||||
### 2. 编译Paho MQTT C++库
|
||||
```cmd
|
||||
git clone https://github.com/eclipse/paho.mqtt.cpp.git
|
||||
cd paho.mqtt.cpp
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DPAHO_MQTT_C_PATH=C:/mqtt/c -DPAHO_BUILD_STATIC=TRUE
|
||||
cmake --build . --config Release
|
||||
cmake --install . --prefix C:/mqtt/cpp
|
||||
```
|
||||
|
||||
### 3. 修改CMakeLists.txt
|
||||
添加库路径:
|
||||
```cmake
|
||||
set(CMAKE_PREFIX_PATH "C:/mqtt/cpp;C:/mqtt/c")
|
||||
find_package(PahoMqttCpp REQUIRED)
|
||||
```
|
||||
|
||||
## 验证安装
|
||||
|
||||
运行以下命令验证:
|
||||
```cmd
|
||||
cd build
|
||||
cmake ..
|
||||
```
|
||||
|
||||
如果没有错误,说明MQTT库已正确配置。
|
||||
|
||||
## 使用示例
|
||||
|
||||
在您的代码中导入MQTT:
|
||||
```cpp
|
||||
#include <mqtt/async_client.h>
|
||||
#include "mqtt.hpp"
|
||||
|
||||
// 创建MQTT客户端
|
||||
auto mqttClient = std::make_unique<custom::MqttClient>("localhost", 1883, "test_client");
|
||||
mqttClient->connect();
|
||||
mqttClient->subscribe("test/topic");
|
||||
```
|
||||
Reference in New Issue
Block a user