Eclipse Paho:MQTT Client C的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/libaineu2004/article/details/82841117

https://www.eclipse.org/paho/downloads.php

eclipse/paho.mqtt.c库(使用v1.3.0)的下载地址是:https://github.com/eclipse/paho.mqtt.c/tree/v1.3.0

1、paho.mqtt.c-1.3.0\CMakeLists.txt,使能PAHO_BUILD_STATIC 和PAHO_BUILD_SAMPLES 

## build options
SET(PAHO_WITH_SSL FALSE CACHE BOOL "Flag that defines whether to build ssl-enabled binaries too. ")
SET(PAHO_BUILD_STATIC TRUE CACHE BOOL "Build static library") #firecat modify
SET(PAHO_BUILD_DOCUMENTATION FALSE CACHE BOOL "Create and install the HTML based API documentation (requires Doxygen)")
SET(PAHO_BUILD_SAMPLES TRUE CACHE BOOL "Build sample programs") #firecat modify
SET(PAHO_BUILD_DEB_PACKAGE FALSE CACHE BOOL "Build debian package")
SET(PAHO_ENABLE_TESTING TRUE CACHE BOOL "Build tests and run")
SET(PAHO_ENABLE_CPACK TRUE CACHE BOOL "Enable CPack")

2、paho.mqtt.c-1.3.0\src\samples\CMakeLists.txt

#*******************************************************************************
#  Copyright (c) 2015, 2017 logi.cals GmbH and others
#
#  All rights reserved. This program and the accompanying materials
#  are made available under the terms of the Eclipse Public License v1.0
#  and Eclipse Distribution License v1.0 which accompany this distribution.
#
#  The Eclipse Public License is available at
#     http://www.eclipse.org/legal/epl-v10.html
#  and the Eclipse Distribution License is available at
#    http://www.eclipse.org/org/documents/edl-v10.php.
#
#  Contributors:
#     Rainer Poisel - initial version
#     Ian Craggs - update sample names
#*******************************************************************************/

# Note: on OS X you should install XCode and the associated command-line tools

## compilation/linkage settings
INCLUDE_DIRECTORIES(
    .
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_BINARY_DIR}
    )

IF (WIN32)
  	ADD_DEFINITIONS(/DCMAKE_BUILD /D_CRT_SECURE_NO_DEPRECATE)
ENDIF()

# sample files c
#ADD_EXECUTABLE(paho_c_pub paho_c_pub.c pubsub_opts.c) #firecat
#ADD_EXECUTABLE(paho_c_sub paho_c_sub.c pubsub_opts.c)
#ADD_EXECUTABLE(paho_cs_pub paho_cs_pub.c pubsub_opts.c)
#ADD_EXECUTABLE(paho_cs_sub paho_cs_sub.c pubsub_opts.c)

#TARGET_LINK_LIBRARIES(paho_c_pub paho-mqtt3as)
#TARGET_LINK_LIBRARIES(paho_c_sub paho-mqtt3as)
#TARGET_LINK_LIBRARIES(paho_cs_pub paho-mqtt3cs)
#TARGET_LINK_LIBRARIES(paho_cs_sub paho-mqtt3cs)

ADD_EXECUTABLE(MQTTAsync_subscribe MQTTAsync_subscribe.c)
ADD_EXECUTABLE(MQTTAsync_publish MQTTAsync_publish.c)
ADD_EXECUTABLE(MQTTClient_subscribe MQTTClient_subscribe.c)
ADD_EXECUTABLE(MQTTClient_publish MQTTClient_publish.c)
ADD_EXECUTABLE(MQTTClient_publish_async MQTTClient_publish_async.c)

TARGET_LINK_LIBRARIES(MQTTAsync_subscribe paho-mqtt3a)
TARGET_LINK_LIBRARIES(MQTTAsync_publish paho-mqtt3a)
TARGET_LINK_LIBRARIES(MQTTClient_subscribe paho-mqtt3c)
TARGET_LINK_LIBRARIES(MQTTClient_publish paho-mqtt3c)
TARGET_LINK_LIBRARIES(MQTTClient_publish_async paho-mqtt3c)

#INSTALL(TARGETS paho_c_sub #firecat
#                paho_c_pub
#                paho_cs_sub
#                paho_cs_pub
#                MQTTAsync_subscribe
#                MQTTAsync_publish
#                MQTTClient_subscribe
#                MQTTClient_publish
#                MQTTClient_publish_async

#    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
#    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

3、使用cmake release编译,生成以下的动态库和静态库文件:

The Paho C client comprises four shared libraries:

libmqttv3a.so - asynchronous
libmqttv3as.so - asynchronous with SSL
libmqttv3c.so - "classic" / synchronous
libmqttv3cs.so - "classic" / synchronous with SSL
Optionally, using the CMake build, you can build static versions of those libraries.

libpaho-mqtt3a.so

libpaho-mqtt3a.so.1

libpaho-mqtt3a.so.1.3.0

libpaho-mqtt3a-static.a

libpaho-mqtt3c.so

libpaho-mqtt3c.so.1

libpaho-mqtt3c.so.1.3.0

libpaho-mqtt3c-static.a

把它们一起拷贝到路径:/usr/local/lib/

此时需要在/etc/ld.so.conf中加入库文件所在的目录:/usr/local/lib/

然后在终端执行命令,使之生效:

[root@localhost etc]# ldconfig

注意,/usr/local/lib/每次有库文件更新,都需要终端重新运行一次ldconfig这条命令。

4、使用案例

MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;
    int ch;

    MQTTClient_create(&client, ADDRESS, CLIENTID,
                      MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.MQTTVersion = MQTTVERSION_3_1_1;
    conn_opts.username = USERNAME;
    conn_opts.password = PASSWORD;

    MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
    MQTTClient_subscribe(client, TOPIC, QOS);

猜你喜欢

转载自blog.csdn.net/libaineu2004/article/details/82841117