Window MQTT lib

环境准备

工具:

源码 (已附在文件夹里):

编译MQTT库

编译MQTT C库paho.mqtt.c

  1. 安装好CMake后,打开CMake-GUI;![image.png](https://img-blog.csdnimg.cn/img_convert/56304347da64a221f33c4fbbe9f84329.png#averageHue=#2d2d2d&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=143&id=u964558b1&margin=[object Object]&name=image.png&originHeight=286&originWidth=516&originalType=binary&ratio=1&rotation=0&showTitle=false&size=54970&status=done&style=none&taskId=ude0f6d4a-1f55-47aa-88f6-b82a998aae0&title=&width=258)
  2. 设置源码目录:”Where is the source code“;设置编译目录:”Where to build the binaries”。点击“Configure”按钮;![image.png](https://img-blog.csdnimg.cn/img_convert/8328437bef84a896334b73e41717ddf4.png#averageHue=#f7f4f3&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=750&id=uaa07ad1e&margin=[object Object]&name=image.png&originHeight=1500&originWidth=1460&originalType=binary&ratio=1&rotation=0&showTitle=false&size=173452&status=done&style=none&taskId=u1f6ee368-f51a-4cff-ab37-a382472fb08&title=&width=730)
  3. 点击"Configure" 后选择generator和系统类型, 点击finish![image.png](https://img-blog.csdnimg.cn/img_convert/2f34cf89fc096ff8f13ab8efa626486e.png#averageHue=#ebdfd9&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=422&id=uc21bc59a&margin=[object Object]&name=image.png&originHeight=844&originWidth=1448&originalType=binary&ratio=1&rotation=0&showTitle=false&size=145073&status=done&style=none&taskId=ue3c13725-c7ba-4140-b6cf-a2c952d4315&title=&width=724)
  4. 此时进入cmake参数配置页面![image.png](https://img-blog.csdnimg.cn/img_convert/4b31d1750ebd769d69c94577243e2a7f.png#averageHue=#f3cece&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=770&id=u483ff320&margin=[object Object]&name=image.png&originHeight=1540&originWidth=1474&originalType=binary&ratio=1&rotation=0&showTitle=false&size=589870&status=done&style=none&taskId=u376be3d2-774b-427b-b8c4-7873d2b86d2&title=&width=737)
  5. 修改部分所需的参数
    1. 按需要修改MQTT C库的安装路径![image.png](https://img-blog.csdnimg.cn/img_convert/7aa4572cc79a4b7578a037077017234a.png#averageHue=#f58686&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=170&id=u6c198c0b&margin=[object Object]&name=image.png&originHeight=340&originWidth=1564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=242895&status=done&style=none&taskId=ua8b1c1c8-46ea-4b9a-9a8c-f217bbdeb0b&title=&width=782)
    2. 修改以下几项![image.png](https://img-blog.csdnimg.cn/img_convert/f4f598ff45de965f02fc5423d9ddb064.png#averageHue=#f68585&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=171&id=ube228da2&margin=[object Object]&name=image.png&originHeight=342&originWidth=1570&originalType=binary&ratio=1&rotation=0&showTitle=false&size=147437&status=done&style=none&taskId=u56c791b6-1426-4073-a4b3-d5e50546479&title=&width=785)
  6. 点击 “Configure”显示以下 “Configuring done”之后,再点击Generate;![image.png](https://img-blog.csdnimg.cn/img_convert/6bd4b5d5ec041006be5a03b9c87608c1.png#averageHue=#ccbeaf&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=654&id=u869e0e7e&margin=[object Object]&name=image.png&originHeight=1308&originWidth=1564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=395543&status=done&style=none&taskId=u883ac7f9-25ec-4be8-aec3-694baa54ad9&title=&width=782)
  7. 此时“Open Project”的按钮应该是处于可点击状态,点击“Open Project”后会使用Visual Studio打开项目:
  8. 在Visual Studio上右击“解决方案”并点击“生成解决方案”![image.png](https://img-blog.csdnimg.cn/img_convert/8822909a8a837a69142af774f55fe3ae.png#averageHue=#69645a&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=644&id=u1fba3ea0&margin=[object Object]&name=image.png&originHeight=1288&originWidth=1288&originalType=binary&ratio=1&rotation=0&showTitle=false&size=573612&status=done&style=none&taskId=ua3f25350-4307-4644-89cd-908f3c8638d&title=&width=644)
  9. 生成成功后,再对“INSTALL”项目右击,并点击“生成”库文件和头文件则会安装到 5.a 步骤 中指定的安装路径中;![image.png](https://img-blog.csdnimg.cn/img_convert/56b76ed4915ff4fcd57571065eb3ac67.png#averageHue=#666257&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=432&id=u08900665&margin=[object Object]&name=image.png&originHeight=864&originWidth=1300&originalType=binary&ratio=1&rotation=0&showTitle=false&size=322644&status=done&style=none&taskId=u691403a0-5d4e-45dc-ad43-5c5f0bc99cb&title=&width=650)

编译MQTT C++库paho.mqtt.cpp

  1. 设置源码目录:”Where is the source code“;设置编译目录:”Where to build the binaries”。点击“Configure”;![image.png](https://img-blog.csdnimg.cn/img_convert/6c8bf78f9c310477060fa3b95102b9f2.png#averageHue=#f6f5f5&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=653&id=ufa10fa1b&margin=[object Object]&name=image.png&originHeight=1306&originWidth=1562&originalType=binary&ratio=1&rotation=0&showTitle=false&size=199179&status=done&style=none&taskId=u6cc7d7ff-359e-4b51-86fa-8a566754e8b&title=&width=781)
  2. 点击"Configure" 后选择generator和系统类型, 点击finish![image.png](https://img-blog.csdnimg.cn/img_convert/86eb4708b6a67502c6b0fe21581cd0e3.png#averageHue=#ebdfd9&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=422&id=yhwPy&margin=[object Object]&name=image.png&originHeight=844&originWidth=1448&originalType=binary&ratio=1&rotation=0&showTitle=false&size=145073&status=done&style=none&taskId=ue3c13725-c7ba-4140-b6cf-a2c952d4315&title=&width=724)
  3. 因为没有配置MQTT C库路径,会报错如下; ![image.png](https://img-blog.csdnimg.cn/img_convert/ba93dea82962580191645413f57b77a8.png#averageHue=#f1d4d4&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=653&id=ubc0d0fa7&margin=[object Object]&name=image.png&originHeight=1306&originWidth=1566&originalType=binary&ratio=1&rotation=0&showTitle=false&size=735543&status=done&style=none&taskId=u9b2d91c1-6522-417d-a8e9-d830e6d6a72&title=&width=783)
  4. 修改CMAKE配置参数,注意配置以下几项
    1. CMAKE_INSTALL_PREFIX: MQTT C++库安装路径;
    2. PAHO_MQTT_C_INCLUDE_DIRS: MQTT C库头文件路径;
    3. PATHO_MQTT_C_LIBRARIES: MQTT C库lib路径;
    4. PAHO_BUILD_SHARED: 创建MQTT C++动态库dll;![image.png](https://img-blog.csdnimg.cn/img_convert/51c7aad0fbfb846d3d8fb47a2a51ea4c.png#averageHue=#f09797&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=209&id=udcc16fc2&margin=[object Object]&name=image.png&originHeight=418&originWidth=1570&originalType=binary&ratio=1&rotation=0&showTitle=false&size=193925&status=done&style=none&taskId=u83dd7af8-e642-46b8-80ad-3c199d42785&title=&width=785)
  5. 再依次点击"Configure" -> “Generate” -> “Open Project”;![image.png](https://img-blog.csdnimg.cn/img_convert/4d9b9fdfb754fc2d53265e981d838bf4.png#averageHue=#f3f2f2&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=652&id=u68e1dc7f&margin=[object Object]&name=image.png&originHeight=1304&originWidth=1566&originalType=binary&ratio=1&rotation=0&showTitle=false&size=475517&status=done&style=none&taskId=uc3086143-0416-4c97-8866-dc787d44041&title=&width=783)
  6. 打开Visual Studio后生成所有解决方案,然后右击"INSTALL"项目再点击"生成",即可将MQTT C++库文件和头文件安装到以上第4步指定的路径;

至此paho.mqtt.c和paho.mqtt.cpp的库已编译安装完成;

创建MQTT客户端

  1. 使用Visual Studio创建C++项目;
  2. 粘贴代码到cpp文件:
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include "mqtt/client.h"

#pragma comment(lib,"ws2_32.lib")

const std::string SERVER_ADDRESS("tcp://broker.emqx.io:1883");
const std::string CLIENT_ID("33f1c750-01a6-4a26-9057-6a5adf0f80f5");
const std::string TOPIC("lanffy/test");
const int QOS = 1;

class user_callback : public virtual mqtt::callback
{
void connection_lost(const std::string& cause) override {
    std::cout << "\nConnection lost" << std::endl;
    if (!cause.empty())
        std::cout << "\tcause: " << cause << std::endl;
}

void delivery_complete(mqtt::delivery_token_ptr tok) override {
    std::cout << "\n\t[Delivery complete for token: "
        << (tok ? tok->get_message_id() : -1) << "]" << std::endl;
}

public:
};


int main(int argc, char* argv[])
{
    std::cout << "Initialzing..." << std::endl;

    mqtt::client client(SERVER_ADDRESS, CLIENT_ID);

    user_callback cb;
    client.set_callback(cb);

    mqtt::connect_options connOpts;
    connOpts.set_keep_alive_interval(20);
    connOpts.set_clean_session(true);
    std::cout << "...OK" << std::endl;


    try {
        std::cout << "\nConnecting..." << std::endl;
        client.connect(connOpts);
        std::cout << "...OK" << std::endl;

        // First use a message pointer.

        std::cout << "\nSending message..." << std::endl;
        auto pubmsg = mqtt::make_message(TOPIC, "Hello World,This is a message...");
        pubmsg->set_qos(QOS);
        client.publish(pubmsg);
        std::cout << "...OK" << std::endl;

        // Disconnect
        std::cout << "\nDisconnecting..." << std::endl;
        client.disconnect();
        std::cout << "...OK" << std::endl;
    }
    catch (const mqtt::persistence_exception& exc) {
        std::cerr << "Persistence Error: " << exc.what() << " ["
            << exc.get_reason_code() << "]" << std::endl;
        return 1;
    }
    catch (const mqtt::exception& exc) {
        std::cerr << exc.what() << std::endl;
        return 1;
    }

    std::cout << "\nExiting" << std::endl;
    return 0;
}
  1. 库配置
    1. 拷贝 paho.mqtt.c 和 paho.mqtt.cpp 中的lib目录下的lib文件到项目文件同级目录下: ![image.png](https://img-blog.csdnimg.cn/img_convert/6d109b6b0d68d100724623c827f9b581.png#averageHue=#f8f2f0&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=173&id=u0f3893ce&margin=[object Object]&name=image.png&originHeight=346&originWidth=1286&originalType=binary&ratio=1&rotation=0&showTitle=true&size=143881&status=done&style=none&taskId=u9189f11b-7c0f-4855-9aba-ca9b5fc90f7&title=MQTT C库路径&width=643 “MQTT C库路径”)![image.png](https://img-blog.csdnimg.cn/img_convert/9cdacf35d043450815daf495946ebe14.png#averageHue=#f9f4f3&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=186&id=u6853d9c1&margin=[object Object]&name=image.png&originHeight=372&originWidth=1510&originalType=binary&ratio=1&rotation=0&showTitle=true&size=165631&status=done&style=none&taskId=udaaf720c-8bd5-4a8e-a848-3c9592bf9a6&title=MQTT C++路径&width=755 “MQTT C++路径”)![image.png](https://img-blog.csdnimg.cn/img_convert/295dce816971a2abeca4c7b517fa4396.png#averageHue=#f8f5f4&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=323&id=uf1640228&margin=[object Object]&name=image.png&originHeight=646&originWidth=1598&originalType=binary&ratio=1&rotation=0&showTitle=true&size=311663&status=done&style=none&taskId=u37923311-41de-4a1d-a33b-59f3b4e8d90&title=项目路径&width=799 “项目路径”)
    2. 右击项目,进入“属性”; ![image.png](https://img-blog.csdnimg.cn/img_convert/7351c34a8cdb10863620362441b2be6c.png#averageHue=#232221&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=659&id=u7fe954c1&margin=[object Object]&name=image.png&originHeight=1318&originWidth=1042&originalType=binary&ratio=1&rotation=0&showTitle=false&size=487066&status=done&style=none&taskId=uc82ff8d1-53bd-402e-b617-dbb5fa7fe78&title=&width=521)
    3. 注意选择平台类型, 左侧选择"C/C++", 然后选择C库和C++库头文件的路径;![image.png](https://img-blog.csdnimg.cn/img_convert/6c261d0ddb28314de4d70dbf46a06b9c.png#averageHue=#f2f1f1&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=509&id=u35ec6626&margin=[object Object]&name=image.png&originHeight=1018&originWidth=1610&originalType=binary&ratio=1&rotation=0&showTitle=false&size=490271&status=done&style=none&taskId=u754b4989-06ff-4893-8d71-09f49c5f261&title=&width=805)![image.png](https://img-blog.csdnimg.cn/img_convert/2b6074b0e8b1e697dd113ace59413ff4.png#averageHue=#ecebeb&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=344&id=ua09b74b0&margin=[object Object]&name=image.png&originHeight=688&originWidth=782&originalType=binary&ratio=1&rotation=0&showTitle=false&size=141557&status=done&style=none&taskId=u7e370723-2b62-4cfe-8712-1d37d3251a7&title=&width=391)
    4. 设置链接器,在"附加依赖项"中填入四个库名(每行一个):![image.png](https://img-blog.csdnimg.cn/img_convert/ac4551056161be034c946171e2e0e819.png#averageHue=#eceaea&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=506&id=uad057d30&margin=[object Object]&name=image.png&originHeight=1012&originWidth=1604&originalType=binary&ratio=1&rotation=0&showTitle=false&size=501962&status=done&style=none&taskId=u5e8a673b-927b-4b20-ac21-928b1315d84&title=&width=802)
paho-mqttpp3.lib
paho-mqttpp3-static.lib
paho-mqtt3a-static.lib 
paho-mqtt3c-static.lib
  1. 确定属性页后返回工程,选择指定的平台类型;![image.png](https://img-blog.csdnimg.cn/img_convert/c80aabb97af37a1ffbcb1a87a47b4b0b.png#averageHue=#49503f&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=340&id=uab7e0f80&margin=[object Object]&name=image.png&originHeight=680&originWidth=1468&originalType=binary&ratio=1&rotation=0&showTitle=false&size=454818&status=done&style=none&taskId=u6928eac0-8548-4c69-abad-7ef20f3ddc5&title=&width=734)
  2. 编译项目![image.png](https://img-blog.csdnimg.cn/img_convert/ca38884ed86775bfb16e4f06b01129d0.png#averageHue=#554c41&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=658&id=u85358005&margin=[object Object]&name=image.png&originHeight=1316&originWidth=1304&originalType=binary&ratio=1&rotation=0&showTitle=false&size=841739&status=done&style=none&taskId=u856ef405-bfd3-4bcc-a432-d5288cef482&title=&width=652)
  3. 运行![image.png](https://img-blog.csdnimg.cn/img_convert/9912c32e6336adaf9341a258fbfd8650.png#averageHue=#484f40&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=305&id=u5c284ec7&margin=[object Object]&name=image.png&originHeight=610&originWidth=1602&originalType=binary&ratio=1&rotation=0&showTitle=false&size=470332&status=done&style=none&taskId=u6c17d68b-2418-4af9-8852-51742fc7655&title=&width=801)
  4. 如果提示找不到dll,到MQTT C++库路径bin目录下把dll文件拷贝到对应的Debug或Release目录下即可![image.png](https://img-blog.csdnimg.cn/img_convert/4d696e51c4bb5ea60092e41dd11e17ec.png#averageHue=#fafafa&clientId=u32b477bd-7544-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=142&id=u3c1e5382&margin=[object Object]&name=image.png&originHeight=284&originWidth=1410&originalType=binary&ratio=1&rotation=0&showTitle=false&size=94677&status=done&style=none&taskId=u7790b5ec-c643-45ff-9624-0603b0f14e9&title=&width=705)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆的菜菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值