交叉编译paho.mqtt.c和paho.mqtt.cpp(MQTT客户端)
一、参考资料
【MQTT】paho.mqtt.cpp 库的 介绍、下载、交叉编译、MQTT客户端例子源码-CSDN博客
【MQTT】paho.mqtt.c 库的“介绍、下载、交叉编译” 详解,以及编写MQTT客户端例子源码-CSDN博客
二、准备工作
1. 重要说明
paho.mqtt.cpp
与paho.mqtt.c
,务必版本对齐,否则导致编译失败。
- 根据
paho.mqtt.cpp
版本来确定paho.mqtt.c
的版本,先交叉编译paho.mqtt.cpp
,然后交叉编译paho.mqtt.c
。
2. 编译环境
- 宿主机:
Ubuntu 20.04.6 LTS
。 - Host:ARM32位。
- 交叉编译器:
arm-linux-gnueabihf-gcc-11.1.0
。
3. 设置交叉编译工具链
在交叉编译之前,需要设置交叉编译工具链的环境变量。
export PATH=/path/to/toolchains/arm-linux-gnueabihf/bin:$PATH
三、交叉编译paho.mqtt.cpp
1. 下载源码
下载源码:GitHub - eclipse-paho/paho.mqtt.cpp
下载并解压源码:
tar -xvzf paho.mqtt.cpp-1.5.2.tar.gz
2. 编译安装
cd paho.mqtt.cpp-1.5.2 && mkdir build# 编译带有 openssl 的库
cmake .. \-DCMAKE_INSTALL_PREFIX=/path/to/paho.mqtt.cpp/arm_install \-DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \-DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \-DPAHO_WITH_MQTT_C=ON \-DPAHO_WITH_SSL=ON \ # 启用 SSL 支持-DOPENSSL_ROOT_DIR=/path/to/openssl/arm_install \ # OpenSSL 的交叉编译后路径-DPAHO_BUILD_STATIC=OFF \-DPAHO_BUILD_SAMPLES=OFF \ # 关闭示例编译-DPAHO_BUILD_TESTS=OFF \ # 关闭测试样例编译-DCMAKE_CXX_FLAGS="-std=c++11"make -j8
make install
安装后的文件目录:
yoyo@yoyo:~/360Downloads/paho.mqtt.cpp-1.5.2$ tree -L 4 arm_install/
arm_install/
├── bin
│ └── MQTTVersion
├── include
│ ├── mqtt
│ │ ├── async_client.h
│ │ ├── buffer_ref.h
│ │ ├── buffer_view.h
│ │ ├── callback.h
│ │ ├── client.h
│ │ ├── connect_options.h
│ │ ├── create_options.h
│ │ ├── delivery_token.h
│ │ ├── disconnect_options.h
│ │ ├── event.h
│ │ ├── exception.h
│ │ ├── export.h
│ │ ├── iaction_listener.h
│ │ ├── iasync_client.h
│ │ ├── iclient_persistence.h
│ │ ├── message.h
│ │ ├── platform.h
│ │ ├── properties.h
│ │ ├── reason_code.h
│ │ ├── response_options.h
│ │ ├── server_response.h
│ │ ├── ssl_options.h
│ │ ├── string_collection.h
│ │ ├── subscribe_options.h
│ │ ├── thread_queue.h
│ │ ├── token.h
│ │ ├── topic.h
│ │ ├── topic_matcher.h
│ │ ├── types.h
│ │ └── will_options.h
│ ├── MQTTAsync.h
│ ├── MQTTClient.h
│ ├── MQTTClientPersistence.h
│ ├── MQTTExportDeclarations.h
│ ├── MQTTProperties.h
│ ├── MQTTReasonCodes.h
│ └── MQTTSubscribeOpts.h
├── lib
│ ├── cmake
│ │ ├── eclipse-paho-mqtt-c
│ │ │ ├── eclipse-paho-mqtt-cConfig.cmake
│ │ │ ├── eclipse-paho-mqtt-cConfig-noconfig.cmake
│ │ │ └── eclipse-paho-mqtt-cConfigVersion.cmake
│ │ └── PahoMqttCpp
│ │ ├── PahoMqttCppConfig.cmake
│ │ ├── PahoMqttCppConfigVersion.cmake
│ │ ├── PahoMqttCppTargets.cmake
│ │ └── PahoMqttCppTargets-noconfig.cmake
│ ├── libpaho-mqtt3a.so -> libpaho-mqtt3a.so.1
│ ├── libpaho-mqtt3a.so.1 -> libpaho-mqtt3a.so.1.3.14
│ ├── libpaho-mqtt3a.so.1.3.14
│ ├── libpaho-mqtt3as.so -> libpaho-mqtt3as.so.1
│ ├── libpaho-mqtt3as.so.1 -> libpaho-mqtt3as.so.1.3.14
│ ├── libpaho-mqtt3as.so.1.3.14
│ ├── libpaho-mqtt3c.so -> libpaho-mqtt3c.so.1
│ ├── libpaho-mqtt3c.so.1 -> libpaho-mqtt3c.so.1.3.14
│ ├── libpaho-mqtt3c.so.1.3.14
│ ├── libpaho-mqtt3cs.so -> libpaho-mqtt3cs.so.1
│ ├── libpaho-mqtt3cs.so.1 -> libpaho-mqtt3cs.so.1.3.14
│ ├── libpaho-mqtt3cs.so.1.3.14
│ ├── libpaho-mqttpp3.so -> libpaho-mqttpp3.so.1
│ ├── libpaho-mqttpp3.so.1 -> libpaho-mqttpp3.so.1.5.1
│ └── libpaho-mqttpp3.so.1.5.1
└── share└── doc└── Eclipse Paho C├── CONTRIBUTING.md├── edl-v10├── epl-v20├── notice.html├── README.md└── samples
cmake 编译:
yoyo@yoyo:~/360Downloads/paho.mqtt.cpp-1.5.2/build$ cmake .. \
> -DCMAKE_INSTALL_PREFIX=/path/to/paho.mqtt.cpp-1.5.2/arm_install \
> -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
> -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \
> -DPAHO_WITH_MQTT_C=ON \
> -DPAHO_WITH_SSL=ON \
> -DOPENSSL_ROOT_DIR=/path/to/openssl-openssl-3.5.0/arm_install \
> -DPAHO_BUILD_STATIC=OFF \
> -DPAHO_BUILD_SAMPLES=OFF \
> -DPAHO_BUILD_TESTS=OFF \
> -DCMAKE_CXX_FLAGS="-std=c++11"
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /path/to/toolchains/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /path/to/toolchains/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenSSL: /path/to/openssl-openssl-3.5.0/arm_install/lib/libcrypto.so (found version "3.5.0")
-- Paho C: Bundled
CMake Deprecation Warning at externals/paho-mqtt-c/CMakeLists.txt:18 (cmake_minimum_required):Compatibility with CMake < 3.10 will be removed from a future version ofCMake.Update the VERSION argument <min> value. Or, use the <min>...<max> syntaxto tell CMake that the project requires at least <min> but has been updatedto work with policies introduced by <max> or earlier.-- CMake version: 3.31.3
-- CMake system name: Linux
-- Timestamp is 2025-04-14T06:10:16Z
-- Using OpenSSL with headers at /path/to/openssl-openssl-3.5.0/arm_install/include
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Creating shared library
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
-- Configuring done (0.8s)
-- Generating done (0.0s)
-- Build files have been written to: /path/to/paho.mqtt.cpp-1.5.2/build
make 编译:
yoyo@yoyo:~/360Downloads/paho.mqtt.cpp-1.5.2/build$ make -j8
[ 1%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTProtocolClient.c.o
[ 2%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTProtocolClient.c.o
[ 5%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Clients.c.o
[ 5%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTTime.c.o
[ 8%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Clients.c.o
[ 8%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTTime.c.o
[ 9%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/utf-8.c.o
[ 10%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTPacket.c.o
[ 12%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/utf-8.c.o
[ 13%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTPacketOut.c.o
[ 14%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Messages.c.o
[ 16%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Tree.c.o
[ 17%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Socket.c.o
[ 18%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTPacket.c.o
[ 20%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Log.c.o
[ 21%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTPersistence.c.o
[ 22%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Thread.c.o
[ 24%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTProtocolOut.c.o
[ 25%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTPacketOut.c.o
[ 26%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Messages.c.o
[ 28%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTPersistenceDefault.c.o
[ 29%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/SocketBuffer.c.o
[ 30%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/LinkedList.c.o
[ 32%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Tree.c.o
[ 33%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTProperties.c.o
[ 34%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/MQTTReasonCodes.c.o
[ 36%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Base64.c.o
[ 37%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/SHA1.c.o
[ 38%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/WebSocket.c.o
[ 40%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_obj.dir/Proxy.c.o
[ 41%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Log.c.o
[ 42%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Socket.c.o
[ 44%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTPersistence.c.o
[ 45%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Thread.c.o
[ 46%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTProtocolOut.c.o
[ 48%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTPersistenceDefault.c.o
[ 49%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/SocketBuffer.c.o
[ 50%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/LinkedList.c.o
[ 50%] Built target common_obj
[ 52%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTProperties.c.o
[ 53%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/MQTTReasonCodes.c.o
[ 54%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Base64.c.o
[ 56%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/SHA1.c.o
[ 57%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/WebSocket.c.o
[ 58%] Building C object externals/paho-mqtt-c/src/CMakeFiles/common_ssl_obj.dir/Proxy.c.o
[ 60%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3a.dir/MQTTAsync.c.o
[ 61%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3a.dir/MQTTAsyncUtils.c.o
[ 62%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3c.dir/MQTTClient.c.o
[ 62%] Built target common_ssl_obj
[ 64%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3cs.dir/MQTTClient.c.o
[ 65%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3as.dir/MQTTAsync.c.o
[ 66%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3as.dir/SSLSocket.c.o
[ 68%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3cs.dir/SSLSocket.c.o
[ 69%] Building C object externals/paho-mqtt-c/src/CMakeFiles/paho-mqtt3as.dir/MQTTAsyncUtils.c.o
[ 70%] Linking C shared library libpaho-mqtt3c.so
[ 72%] Linking C shared library libpaho-mqtt3a.so
[ 72%] Built target paho-mqtt3c
[ 72%] Built target paho-mqtt3a
[ 73%] Building C object externals/paho-mqtt-c/src/CMakeFiles/MQTTVersion.dir/MQTTVersion.c.o
[ 74%] Linking C executable MQTTVersion
[ 74%] Built target MQTTVersion
[ 76%] Linking C shared library libpaho-mqtt3cs.so
[ 77%] Linking C shared library libpaho-mqtt3as.so
[ 77%] Built target paho-mqtt3cs
[ 77%] Built target paho-mqtt3as
[ 78%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/async_client.cpp.o
[ 80%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/client.cpp.o
[ 81%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/create_options.cpp.o
[ 82%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/connect_options.cpp.o
[ 84%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/disconnect_options.cpp.o
[ 85%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/iclient_persistence.cpp.o
[ 86%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/message.cpp.o
[ 88%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/properties.cpp.o
[ 89%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/reason_code.cpp.o
[ 90%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/response_options.cpp.o
[ 92%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/server_response.cpp.o
[ 93%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/ssl_options.cpp.o
[ 94%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/string_collection.cpp.o
[ 96%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/token.cpp.o
[ 97%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/topic.cpp.o
[ 98%] Building CXX object src/CMakeFiles/paho-mqttpp3-shared.dir/will_options.cpp.o
[100%] Linking CXX shared library libpaho-mqttpp3.so
[100%] Built target paho-mqttpp3-shared
make install 安装:
yoyo@yoyo:~/360Downloads/paho.mqtt.cpp-1.5.2/build$ make install
[ 29%] Built target common_obj
[ 33%] Built target paho-mqtt3a
[ 36%] Built target paho-mqtt3c
[ 38%] Built target MQTTVersion
[ 68%] Built target common_ssl_obj
[ 72%] Built target paho-mqtt3cs
[ 77%] Built target paho-mqtt3as
[100%] Built target paho-mqttpp3-shared
Install the project...
-- Install configuration: ""
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3a.so.1.3.14
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3a.so.1
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3a.so
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so.1.3.14
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so.1
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so.1.3.14" to ""
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/export.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/MQTTAsync_publish.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/MQTTAsync_publish_time.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/MQTTAsync_subscribe.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/MQTTClient_publish.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/MQTTClient_publish_async.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/MQTTClient_subscribe.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/paho_c_pub.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/paho_c_sub.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/paho_cs_pub.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/paho_cs_sub.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/samples/pubsub_opts.c
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/CONTRIBUTING.md
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/epl-v20
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/edl-v10
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/README.md
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/share/doc/Eclipse Paho C/notice.html
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3c.so.1.3.14
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3c.so.1
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3c.so
-- Up-to-date: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3a.so.1.3.14
-- Up-to-date: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3a.so.1
-- Up-to-date: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3a.so
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/bin/MQTTVersion
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.cpp-1.5.2/arm_install/bin/MQTTVersion" to ""
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTAsync.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTClient.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTClientPersistence.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTProperties.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTReasonCodes.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTSubscribeOpts.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/MQTTExportDeclarations.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3cs.so.1.3.14
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3cs.so.1
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3cs.so.1.3.14" to ""
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3cs.so
-- Up-to-date: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so.1.3.14
-- Up-to-date: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so.1
-- Up-to-date: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqtt3as.so
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig.cmake
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig-noconfig.cmake
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfigVersion.cmake
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/async_client.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/buffer_ref.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/buffer_view.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/callback.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/client.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/connect_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/create_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/delivery_token.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/disconnect_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/event.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/exception.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/export.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/iaction_listener.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/iasync_client.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/iclient_persistence.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/message.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/platform.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/properties.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/reason_code.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/response_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/server_response.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/ssl_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/string_collection.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/subscribe_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/thread_queue.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/token.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/topic_matcher.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/topic.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/types.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/mqtt/will_options.h
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqttpp3.so.1.5.1
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqttpp3.so.1
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqttpp3.so.1.5.1" to ""
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/libpaho-mqttpp3.so
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/PahoMqttCpp/PahoMqttCppTargets.cmake
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/PahoMqttCpp/PahoMqttCppTargets-noconfig.cmake
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/PahoMqttCpp/PahoMqttCppConfig.cmake
-- Installing: /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/cmake/PahoMqttCpp/PahoMqttCppConfigVersion.cmake
3. 移植到开发板
cp -arf /path/to/paho.mqtt.cpp/arm_install/lib/ /usr/paho.mqtt.cpp/
4. 简单示例
4.1 订阅——async_subscribe.cpp
这是使用了 libpaho-mqttpp3.so
进行订阅消息的源码,源码路径在源码的这个路径:paho.mqtt.cpp-1.5.2/examples/async_subscribe.cpp
,只更改了服务器地址。完整代码如下:
// async_subscribe.cpp
//
// This is a Paho MQTT C++ client, sample application.
//
// This application is an MQTT subscriber using the C++ asynchronous client
// interface, employing callbacks to receive messages and status updates.
//
// The sample demonstrates:
// - Connecting to an MQTT server/broker using MQTT v3.
// - Subscribing to a topic
// - Receiving messages through the callback API
// - Receiving network disconnect updates and attempting manual reconnects.
// - Using a "clean session" and manually re-subscribing to topics on
// reconnect.
///******************************************************************************** Copyright (c) 2013-2025 Frank Pagliughi <fpagliughi@mindspring.com>** All rights reserved. This program and the accompanying materials* are made available under the terms of the Eclipse Public License v2.0* and Eclipse Distribution License v1.0 which accompany this distribution.** The Eclipse Public License is available at* http://www.eclipse.org/legal/epl-v20.html* and the Eclipse Distribution License is available at* http://www.eclipse.org/org/documents/edl-v10.php.** Contributors:* Frank Pagliughi - initial implementation and documentation*******************************************************************************/#include <cctype>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <thread>#include "mqtt/async_client.h"const std::string DFLT_SERVER_URI("192.168.33.1:1883");
//const std::string DFLT_SERVER_URI("mqtt://localhost:1883");
const std::string CLIENT_ID("paho_cpp_async_subscribe");const std::string TOPIC("#");const int QOS = 1;
const int N_RETRY_ATTEMPTS = 5;/// Callbacks for the success or failures of requested actions.
// This could be used to initiate further action, but here we just log the
// results to the console.class action_listener : public virtual mqtt::iaction_listener
{std::string name_;void on_failure(const mqtt::token& tok) override{std::cout << name_ << " failure";if (tok.get_message_id() != 0)std::cout << " for token: [" << tok.get_message_id() << "]" << std::endl;std::cout << std::endl;}void on_success(const mqtt::token& tok) override{std::cout << name_ << " success";if (tok.get_message_id() != 0)std::cout << " for token: [" << tok.get_message_id() << "]" << std::endl;auto top = tok.get_topics();if (top && !top->empty())std::cout << "\ttoken topic: '" << (*top)[0] << "', ..." << std::endl;std::cout << std::endl;}public:action_listener(const std::string& name) : name_(name) {}
};//*** Local callback & listener class for use with the client connection.* This is primarily intended to receive messages, but it will also monitor* the connection to the broker. If the connection is lost, it will attempt* to restore the connection and re-subscribe to the topic.*/
class callback : public virtual mqtt::callback, public virtual mqtt::iaction_listener{// Counter for the number of connection retriesint nretry_;// The MQTT clientmqtt::async_client& cli_;// Options to use if we need to reconnectmqtt::connect_options& connOpts_;// An action listener to display the result of actions.action_listener subListener_;// This deomonstrates manually reconnecting to the broker by calling// connect() again. This is a possibility for an application that keeps// a copy of it's original connect_options, or if the app wants to// reconnect with different options.// Another way this can be done manually, if using the same options, is// to just call the async_client::reconnect() method.void reconnect(){std::this_thread::sleep_for(std::chrono::milliseconds(2500));try {cli_.connect(connOpts_, nullptr, *this);}catch (const mqtt::exception& exc) {std::cerr << "Error: " << exc.what() << std::endl;exit(1);}}// Re-connection failurevoid on_failure(const mqtt::token& tok) override{std::cout << "Connection attempt failed" << std::endl;if (++nretry_ > N_RETRY_ATTEMPTS)exit(1);reconnect();}// (Re)connection success// Either this or connected() can be used for callbacks.void on_success(const mqtt::token& tok) override {}// (Re)connection successvoid connected(const std::string& cause) override{std::cout << "\nConnection success" << std::endl;std::cout << "\nSubscribing to topic '" << TOPIC << "'\n"<< "\tfor client " << CLIENT_ID << " using QoS" << QOS << "\n"<< "\nPress Q<Enter> to quit\n"<< std::endl;cli_.subscribe(TOPIC, QOS, nullptr, subListener_);}// Callback for when the connection is lost.// This will initiate the attempt to manually reconnect.void connection_lost(const std::string& cause) override{std::cout << "\nConnection lost" << std::endl;if (!cause.empty())std::cout << "\tcause: " << cause << std::endl;std::cout << "Reconnecting..." << std::endl;nretry_ = 0;reconnect();}// Callback for when a message arrives.void message_arrived(mqtt::const_message_ptr msg) override{std::cout << "Message arrived" << std::endl;std::cout << "\ttopic: '" << msg->get_topic() << "'" << std::endl;std::cout << "\tpayload: '" << msg->to_string() << "'\n" << std::endl;}void delivery_complete(mqtt::delivery_token_ptr token) override {}public:callback(mqtt::async_client& cli, mqtt::connect_options& connOpts): nretry_(0), cli_(cli), connOpts_(connOpts), subListener_("Subscription"){}
};/int main(int argc, char* argv[])
{// A subscriber often wants the server to remember its messages when its// disconnected. In that case, it needs a unique ClientID and a// non-clean session.auto serverURI = (argc > 1) ? std::string{argv[1]} : DFLT_SERVER_URI;mqtt::async_client cli(serverURI, CLIENT_ID);mqtt::connect_options connOpts;connOpts.set_clean_session(false);// Install the callback(s) before connecting.callback cb(cli, connOpts);cli.set_callback(cb);// Start the connection.// When completed, the callback will subscribe to topic.try {std::cout << "Connecting to the MQTT server '" << serverURI << "'..." << std::flush;cli.connect(connOpts, nullptr, cb);}catch (const mqtt::exception& exc) {std::cerr << "\nERROR: Unable to connect to MQTT server: '" << serverURI << "'" << exc<< std::endl;return 1;}// Just block till user tells us to quit.while (std::tolower(std::cin.get()) != 'q');// Disconnecttry {std::cout << "\nDisconnecting from the MQTT server..." << std::flush;cli.disconnect()->wait();std::cout << "OK" << std::endl;}catch (const mqtt::exception& exc) {std::cerr << exc << std::endl;return 1;}return 0;
}
交叉编译:
arm-linux-gnueabihf-g++ async_subscribe.cpp -I /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/ -I /path/to/paho.mqtt.c-1.3.14/arm_install/include/ -I /path/to/openssl-openssl-3.5.0/arm_install/include/ -L /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/ -l paho-mqttpp3 -l paho-mqtt3a -l paho-mqtt3as -L /path/to/paho.mqtt.c-1.3.14/arm_install/lib/ -L /path/to/openssl-openssl-3.5.0/arm_install/lib/ -l crypto -l ssl -o async_subscribe
在arm开发板中运行:
/customer/mqtt_test # ./async_subscribe
Connecting to the MQTT server '192.168.33.1:1883'...
Connection successSubscribing to topic '#'for client paho_cpp_async_subscribe using QoS1Press Q<Enter> to quitMessage arrivedtopic: 'hello'payload: 'Hello World!'Message arrivedtopic: 'hello'payload: 'Hi there!'Subscription success for token: [1]token topic: '#', ...
4.2 发布——async_publish.cpp
这是使用了 libpaho-mqttpp3.so
进行发布消息的源码,源码路径在源码的这个路径:paho.mqtt.cpp-1.5.2/examples/async_publish.cpp
,只更改了服务器地址。完整代码如下:
// async_publish.cpp
//
// This is a Paho MQTT C++ client, sample application.
//
// It's an example of how to send messages as an MQTT publisher using the
// C++ asynchronous client interface.
//
// The sample demonstrates:
// - Connecting to an MQTT server/broker
// - Using a connect timeout
// - Publishing messages
// - Default file persistence
// - Last will and testament
// - Using asynchronous tokens
// - Implementing callbacks and action listeners
///******************************************************************************** Copyright (c) 2013-2023 Frank Pagliughi <fpagliughi@mindspring.com>** All rights reserved. This program and the accompanying materials* are made available under the terms of the Eclipse Public License v2.0* and Eclipse Distribution License v1.0 which accompany this distribution.** The Eclipse Public License is available at* http://www.eclipse.org/legal/epl-v20.html* and the Eclipse Distribution License is available at* http://www.eclipse.org/org/documents/edl-v10.php.** Contributors:* Frank Pagliughi - initial implementation and documentation*******************************************************************************/#include <atomic>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <thread>#include "mqtt/async_client.h"using namespace std;
using namespace std::chrono;const string DFLT_SERVER_URI{"192.168.33.1:1883"};
//const string DFLT_SERVER_URI{"mqtt://localhost:1883"};
const string CLIENT_ID{"paho_cpp_async_publish"};const mqtt::persistence_type PERSIST_DIR{"./persist"};const string TOPIC{"hello"};const string PAYLOAD1{"Hello World!"};
const string PAYLOAD2{"Hi there!"};
const string PAYLOAD3{"Is anyone listening?"};
const string PAYLOAD4{"Someone is always listening."};const string LWT_PAYLOAD{"Last will and testament."};const int QOS = 1;const auto TIMEOUT = std::chrono::seconds(10);//*** A callback class for use with the main MQTT client.*/
class callback : public virtual mqtt::callback
{
public:void connection_lost(const string& cause) override{cout << "\nConnection lost" << endl;if (!cause.empty())cout << "\tcause: " << cause << endl;}void delivery_complete(mqtt::delivery_token_ptr tok) override{cout << "\tDelivery complete for token: " << (tok ? tok->get_message_id() : -1)<< endl;}
};//*** A base action listener.*/
class action_listener : public virtual mqtt::iaction_listener
{
protected:void on_failure(const mqtt::token& tok) override{cout << "\tListener failure for token: " << tok.get_message_id() << endl;}void on_success(const mqtt::token& tok) override{cout << "\tListener success for token: " << tok.get_message_id() << endl;}
};//*** A derived action listener for publish events.*/
class delivery_action_listener : public action_listener
{atomic<bool> done_;void on_failure(const mqtt::token& tok) override{action_listener::on_failure(tok);done_ = true;}void on_success(const mqtt::token& tok) override{action_listener::on_success(tok);done_ = true;}public:delivery_action_listener() : done_(false) {}bool is_done() const { return done_; }
};/int main(int argc, char* argv[])
{// A client that just publishes normally doesn't need a persistent// session or Client ID unless it's using persistence, then the local// library requires an ID to identify the persistence files.string serverURI = (argc > 1) ? string{argv[1]} : DFLT_SERVER_URI,clientID = (argc > 2) ? string{argv[2]} : CLIENT_ID;cout << "Initializing for server '" << serverURI << "'..." << endl;mqtt::async_client client(serverURI, clientID, PERSIST_DIR);callback cb;client.set_callback(cb);auto connOpts = mqtt::connect_options_builder().connect_timeout(5s).clean_session().will(mqtt::message(TOPIC, LWT_PAYLOAD, QOS, false)).finalize();cout << " ...OK" << endl;try {cout << "\nConnecting..." << endl;mqtt::token_ptr conntok = client.connect(connOpts);cout << "Waiting for the connection..." << endl;conntok->wait();cout << " ...OK" << endl;// First use a message pointer.cout << "\nSending message..." << endl;mqtt::message_ptr pubmsg = mqtt::make_message(TOPIC, PAYLOAD1);pubmsg->set_qos(QOS);client.publish(pubmsg)->wait_for(TIMEOUT);cout << " ...OK" << endl;// Now try with itemized publish.cout << "\nSending next message..." << endl;mqtt::delivery_token_ptr pubtok;pubtok = client.publish(TOPIC, PAYLOAD2, QOS, false);cout << " ...with token: " << pubtok->get_message_id() << endl;cout << " ...for message with " << pubtok->get_message()->get_payload().size()<< " bytes" << endl;pubtok->wait_for(TIMEOUT);cout << " ...OK" << endl;// Now try with a listenercout << "\nSending next message..." << endl;action_listener listener;pubmsg = mqtt::make_message(TOPIC, PAYLOAD3);pubtok = client.publish(pubmsg, nullptr, listener);pubtok->wait();cout << " ...OK" << endl;// Finally try with a listener, but no tokencout << "\nSending final message..." << endl;delivery_action_listener deliveryListener;pubmsg = mqtt::make_message(TOPIC, PAYLOAD4);client.publish(pubmsg, nullptr, deliveryListener);while (!deliveryListener.is_done()) {this_thread::sleep_for(std::chrono::milliseconds(100));}cout << "OK" << endl;// Double check that there are no pending tokensauto toks = client.get_pending_delivery_tokens();if (!toks.empty())cout << "Error: There are pending delivery tokens!" << endl;// Disconnectcout << "\nDisconnecting..." << endl;client.disconnect()->wait();cout << " ...OK" << endl;}catch (const mqtt::exception& exc) {cerr << exc.what() << endl;return 1;}return 0;
}
交叉编译:
arm-linux-gnueabihf-g++ async_publish.cpp -I /path/to/paho.mqtt.cpp-1.5.2/arm_install/include/ -I /path/to/paho.mqtt.c-1.3.14/arm_install/include/ -I /path/to/openssl-openssl-3.5.0/arm_install/include/ -L /path/to/paho.mqtt.cpp-1.5.2/arm_install/lib/ -l paho-mqttpp3 -l paho-mqtt3a -l paho-mqtt3as -L /path/to/paho.mqtt.c-1.3.14/arm_install/lib/ -L /path/to/openssl-openssl-3.5.0/arm_install/lib/ -l crypto -l ssl -l pthread -o async_publish
在arm开发板中运行:
/customer/mqtt_test # ./async_publish
Initializing for server '192.168.33.1:1883'......OKConnecting...
Waiting for the connection......OKSending message......OKSending next message...Delivery complete for token: 1...with token: 2...for message with 9 bytesDelivery complete for token: 2...OKSending next message...Listener success for token: 0...OKSending final message...Listener success for token: 0
OKDisconnecting......OK
四、交叉编译paho.mqtt.c
1. 下载源码
下载源码:[GitHub - eclipse-paho/paho.mqtt.c](https://eclipse-paho.github.io/paho.mqtt.c/ https://github.com/eclipse-paho/paho.mqtt.c)
git clone https://github.com/eclipse-paho/paho.mqtt.c.git
2. 编译安装
mkdir build && cd buildcmake ..\-DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=/path/to/paho.mqtt.c/arm_install-DCMAKE_SYSTEM_NAME=Linux \-DCMAKE_SYSTEM_PROCESSOR=arm \ # 目标平台架构(如 arm/aarch64)-DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \-DPAHO_WITH_SSL=ON \ # 启用 SSL 支持-DOPENSSL_ROOT_DIR=/path/to/openssl/arm_install \ # OpenSSL 的交叉编译后路径-DPAHO_BUILD_STATIC=OFF \ # 生成静态库-DPAHO_BUILD_DOCUMENTATION=OFF \ # 关闭文档生成-DPAHO_BUILD_SAMPLES=OFF # 关闭示例编译make -j8
make install
编译安装后的文件目录:
yoyo@yoyo:~/360Downloads/paho.mqtt.c-1.3.14$ tree -L 2 arm_install/
arm_install/
├── bin
│ └── MQTTVersion
├── include
│ ├── MQTTAsync.h
│ ├── MQTTClient.h
│ ├── MQTTClientPersistence.h
│ ├── MQTTExportDeclarations.h
│ ├── MQTTProperties.h
│ ├── MQTTReasonCodes.h
│ └── MQTTSubscribeOpts.h
├── lib
│ ├── cmake
│ ├── libpaho-mqtt3a.so -> libpaho-mqtt3a.so.1
│ ├── libpaho-mqtt3a.so.1 -> libpaho-mqtt3a.so.1.3.14
│ ├── libpaho-mqtt3a.so.1.3.14
│ ├── libpaho-mqtt3as.so -> libpaho-mqtt3as.so.1
│ ├── libpaho-mqtt3as.so.1 -> libpaho-mqtt3as.so.1.3.14
│ ├── libpaho-mqtt3as.so.1.3.14
│ ├── libpaho-mqtt3c.so -> libpaho-mqtt3c.so.1
│ ├── libpaho-mqtt3c.so.1 -> libpaho-mqtt3c.so.1.3.14
│ ├── libpaho-mqtt3c.so.1.3.14
│ ├── libpaho-mqtt3cs.so -> libpaho-mqtt3cs.so.1
│ ├── libpaho-mqtt3cs.so.1 -> libpaho-mqtt3cs.so.1.3.14
│ └── libpaho-mqtt3cs.so.1.3.14
└── share└── doc
paho.mqtt.c
库编译后会生成四个动态库:
libpaho-mqtt3a.so
:异步
的paho.mqtt.c
库。libpaho-mqtt3as.so
:异步
的、使用了SSL的paho.mqtt.c库。libpaho-mqtt3c.so
:经典的、同步
的paho.mqtt.c库。libpaho-mqtt3cs.so
:经典的、同步
的、使用了SSL的paho.mqtt.c
库。
异步和同步库的区别:Paho MQTT C Client Library: Asynchronous vs synchronous client applications
cmake编译:
yoyo@yoyo:~/360Downloads/paho.mqtt.c-1.3.14/build$ cmake -DCMAKE_BUILD_TYPE=Release \
> -DCMAKE_INSTALL_PREFIX=/path/to/paho.mqtt.c-1.3.14/arm_install \
> -DCMAKE_SYSTEM_NAME=Linux \
> -DCMAKE_SYSTEM_PROCESSOR=arm \
> -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
> -DPAHO_WITH_SSL=ON \
> -DOPENSSL_ROOT_DIR=/path/to/openssl-openssl-3.5.0/arm_install \
> -DPAHO_BUILD_STATIC=OFF \
> -DPAHO_BUILD_DOCUMENTATION=OFF \
> -DPAHO_BUILD_SAMPLES=OFF ..
CMake Deprecation Warning at CMakeLists.txt:18 (cmake_minimum_required):Compatibility with CMake < 3.10 will be removed from a future version ofCMake.Update the VERSION argument <min> value. Or, use the <min>...<max> syntaxto tell CMake that the project requires at least <min> but has been updatedto work with policies introduced by <max> or earlier.-- The C compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /path/to/toolchains/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- CMake version: 3.31.3
-- CMake system name: Linux
-- Timestamp is 2025-04-14T03:30:34Z
-- Found OpenSSL: /path/to/openssl-openssl-3.5.0/arm_install/lib/libcrypto.so (found version "3.5.0")
-- Using OpenSSL with headers at /path/to/openssl-openssl-3.5.0/arm_install/include
-- Configuring done (0.2s)
-- Generating done (0.1s)
-- Build files have been written to: /path/to/paho.mqtt.c-1.3.14/build
make 编译:
yoyo@yoyo:~/360Downloads/paho.mqtt.c-1.3.14/build$ make -j8
[ 3%] Built target thread
[ 27%] Built target common_obj
[ 52%] Built target common_ssl_obj
[ 53%] Building C object src/CMakeFiles/paho-mqtt3c.dir/MQTTClient.c.o
[ 54%] Building C object src/CMakeFiles/paho-mqtt3a.dir/MQTTAsync.c.o
[ 55%] Building C object src/CMakeFiles/paho-mqtt3cs.dir/MQTTClient.c.o
[ 56%] Building C object src/CMakeFiles/paho-mqtt3as.dir/MQTTAsync.c.o
[ 58%] Linking C shared library libpaho-mqtt3a.so
[ 59%] Built target paho-mqtt3a
[ 60%] Linking C executable test4
[ 61%] Linking C executable test45
[ 62%] Linking C executable test6
[ 63%] Linking C executable test8
[ 64%] Linking C executable test9
[ 65%] Built target test6
[ 65%] Built target test45
[ 66%] Built target test4
[ 67%] Linking C executable test95
[ 69%] Linking C shared library libpaho-mqtt3as.so
[ 69%] Built target test8
[ 72%] Linking C executable test11
[ 72%] Built target test9
[ 73%] Linking C executable test_issue373
[ 74%] Built target test95
[ 75%] Built target test11
[ 76%] Built target test_issue373
[ 77%] Built target paho-mqtt3as
[ 78%] Linking C executable test5
[ 79%] Linking C shared library libpaho-mqtt3c.so
[ 79%] Built target paho-mqtt3c
[ 80%] Linking C executable test1
[ 81%] Built target test5
[ 82%] Linking C executable test2
[ 83%] Linking C executable test_sync_session_present
[ 84%] Linking C executable MQTTVersion
[ 86%] Linking C executable test15
[ 87%] Linking C executable test10
[ 88%] Linking C executable test_connect_destroy
[ 89%] Built target test15
[ 90%] Built target test2
[ 91%] Built target test_sync_session_present
[ 92%] Built target MQTTVersion
[ 93%] Built target test1
[ 94%] Built target test10
[ 95%] Built target test_connect_destroy
[ 96%] Linking C shared library libpaho-mqtt3cs.so
[ 97%] Built target paho-mqtt3cs
[ 98%] Linking C executable test3
[100%] Built target test3
make install 安装:
yoyo@yoyo:~/360Downloads/paho.mqtt.c-1.3.14/build$ make install
[ 24%] Built target common_obj
[ 27%] Built target paho-mqtt3a
[ 30%] Built target paho-mqtt3c
[ 32%] Built target MQTTVersion
[ 56%] Built target common_ssl_obj
[ 60%] Built target paho-mqtt3cs
[ 63%] Built target paho-mqtt3as
[ 66%] Built target thread
[ 68%] Built target test1
[ 70%] Built target test15
[ 73%] Built target test2
[ 75%] Built target test3
[ 77%] Built target test4
[ 78%] Built target test45
[ 80%] Built target test5
[ 82%] Built target test6
[ 84%] Built target test8
[ 87%] Built target test9
[ 89%] Built target test95
[ 91%] Built target test10
[ 93%] Built target test11
[ 95%] Built target test_issue373
[ 97%] Built target test_sync_session_present
[100%] Built target test_connect_destroy
Install the project...
-- Install configuration: "Release"
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/MQTTAsync_publish.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/MQTTAsync_publish_time.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/MQTTAsync_subscribe.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/MQTTClient_publish.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/MQTTClient_publish_async.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/MQTTClient_subscribe.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/paho_c_pub.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/paho_c_sub.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/paho_cs_pub.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/paho_cs_sub.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/samples/pubsub_opts.c
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/CONTRIBUTING.md
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/epl-v20
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/edl-v10
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/README.md
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/share/doc/Eclipse Paho C/notice.html
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3c.so.1.3.14
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3c.so.1
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3c.so
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3a.so.1.3.14
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3a.so.1
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3a.so
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/bin/MQTTVersion
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.c-1.3.14/arm_install/bin/MQTTVersion" to ""
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTAsync.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTClient.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTClientPersistence.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTProperties.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTReasonCodes.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTSubscribeOpts.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/include/MQTTExportDeclarations.h
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3cs.so.1.3.14
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3cs.so.1
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3cs.so.1.3.14" to ""
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3cs.so
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3as.so.1.3.14
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3as.so.1
-- Set non-toolchain portion of runtime path of "/path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3as.so.1.3.14" to ""
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/libpaho-mqtt3as.so
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig.cmake
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig-release.cmake
-- Installing: /path/to/paho.mqtt.c-1.3.14/arm_install/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfigVersion.cmake
3. 移植到开发板
cp -arf /path/to/paho.mqtt.cpp/arm_install/lib/ /usr/paho.mqtt.cpp/
4. 简单示例
4.1 订阅——MQTTAsync_subscribe.c
这是使用了 libpaho-mqtt3a.so
进行订阅消息的源码,源码路径在源码的这个路径:paho.mqtt.c-1.3.14/src/samples/MQTTAsync_subscribe.c
,只更改了服务器地址。完整代码如下:
/******************************************************************************** Copyright (c) 2012, 2022 IBM Corp., Ian Craggs** All rights reserved. This program and the accompanying materials* are made available under the terms of the Eclipse Public License v2.0* and Eclipse Distribution License v1.0 which accompany this distribution. ** The Eclipse Public License is available at * https://www.eclipse.org/legal/epl-2.0/* and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php.** Contributors:* Ian Craggs - initial contribution*******************************************************************************/#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif#define ADDRESS "192.168.33.1:1883"
//#define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000Lint disc_finished = 0;
int subscribed = 0;
int finished = 0;void onConnect(void* context, MQTTAsync_successData* response);
void onConnectFailure(void* context, MQTTAsync_failureData* response);void connlost(void *context, char *cause)
{MQTTAsync client = (MQTTAsync)context;MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;int rc;printf("\nConnection lost\n");if (cause)printf(" cause: %s\n", cause);printf("Reconnecting\n");conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;conn_opts.onSuccess = onConnect;conn_opts.onFailure = onConnectFailure;if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS){printf("Failed to start connect, return code %d\n", rc);finished = 1;}
}int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{printf("Message arrived\n");printf(" topic: %s\n", topicName);printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);MQTTAsync_freeMessage(&message);MQTTAsync_free(topicName);return 1;
}void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
{printf("Disconnect failed, rc %d\n", response->code);disc_finished = 1;
}void onDisconnect(void* context, MQTTAsync_successData* response)
{printf("Successful disconnection\n");disc_finished = 1;
}void onSubscribe(void* context, MQTTAsync_successData* response)
{printf("Subscribe succeeded\n");subscribed = 1;
}void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{printf("Subscribe failed, rc %d\n", response->code);finished = 1;
}void onConnectFailure(void* context, MQTTAsync_failureData* response)
{printf("Connect failed, rc %d\n", response->code);finished = 1;
}void onConnect(void* context, MQTTAsync_successData* response)
{MQTTAsync client = (MQTTAsync)context;MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;int rc;printf("Successful connection\n");printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n""Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);opts.onSuccess = onSubscribe;opts.onFailure = onSubscribeFailure;opts.context = client;if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS){printf("Failed to start subscribe, return code %d\n", rc);finished = 1;}
}int main(int argc, char* argv[])
{MQTTAsync client;MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;int rc;int ch;const char* uri = (argc > 1) ? argv[1] : ADDRESS;printf("Using server at %s\n", uri);if ((rc = MQTTAsync_create(&client, uri, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))!= MQTTASYNC_SUCCESS){printf("Failed to create client, return code %d\n", rc);rc = EXIT_FAILURE;goto exit;}if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS){printf("Failed to set callbacks, return code %d\n", rc);rc = EXIT_FAILURE;goto destroy_exit;}conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;conn_opts.onSuccess = onConnect;conn_opts.onFailure = onConnectFailure;conn_opts.context = client;if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS){printf("Failed to start connect, return code %d\n", rc);rc = EXIT_FAILURE;goto destroy_exit;}while (!subscribed && !finished)#if defined(_WIN32)Sleep(100);#elseusleep(10000L);#endifif (finished)goto exit;do {ch = getchar();} while (ch!='Q' && ch != 'q');disc_opts.onSuccess = onDisconnect;disc_opts.onFailure = onDisconnectFailure;if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS){printf("Failed to start disconnect, return code %d\n", rc);rc = EXIT_FAILURE;goto destroy_exit;}while (!disc_finished){#if defined(_WIN32)Sleep(100);#elseusleep(10000L);#endif}destroy_exit:MQTTAsync_destroy(&client);
exit:return rc;
}
交叉编译:
arm-linux-gnueabihf-gcc MQTTAsync_subscribe.c -I /path/to/paho.mqtt.c-1.3.14/arm_install/include/ -L /path/to/paho.mqtt.c-1.3.14/arm_install/lib/ -l paho-mqtt3a -o MQTTAsync_subscribe
在arm开发板中运行:
/customer/mqtt_test # Using server at 192.168.33.1:1883
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1Press Q<Enter> to quitSubscribe succeeded
4.2 发布——MQTTAsync_publish.c
这是使用了 libpaho-mqtt3a.so
进行发布消息的源码,源码路径在源码的这个路径:paho.mqtt.c-1.3.14/src/samples/MQTTAsync_publish.c
,只更改了服务器地址。完整代码如下:
/******************************************************************************** Copyright (c) 2012, 2023 IBM Corp., Ian Craggs** All rights reserved. This program and the accompanying materials* are made available under the terms of the Eclipse Public License v2.0* and Eclipse Distribution License v1.0 which accompany this distribution. ** The Eclipse Public License is available at * https://www.eclipse.org/legal/epl-2.0/* and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php.** Contributors:* Ian Craggs - initial contribution*******************************************************************************/#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif#define ADDRESS "192.168.33.1:1883"
//#define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000Lint finished = 0;void connlost(void *context, char *cause)
{MQTTAsync client = (MQTTAsync)context;MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;int rc;printf("\nConnection lost\n");if (cause)printf(" cause: %s\n", cause);printf("Reconnecting\n");conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS){printf("Failed to start connect, return code %d\n", rc);finished = 1;}
}void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
{printf("Disconnect failed\n");finished = 1;
}void onDisconnect(void* context, MQTTAsync_successData* response)
{printf("Successful disconnection\n");finished = 1;
}void onSendFailure(void* context, MQTTAsync_failureData* response)
{MQTTAsync client = (MQTTAsync)context;MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;int rc;printf("Message send failed token %d error code %d\n", response->token, response->code);opts.onSuccess = onDisconnect;opts.onFailure = onDisconnectFailure;opts.context = client;if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS){printf("Failed to start disconnect, return code %d\n", rc);exit(EXIT_FAILURE);}
}void onSend(void* context, MQTTAsync_successData* response)
{MQTTAsync client = (MQTTAsync)context;MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;int rc;printf("Message with token value %d delivery confirmed\n", response->token);opts.onSuccess = onDisconnect;opts.onFailure = onDisconnectFailure;opts.context = client;if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS){printf("Failed to start disconnect, return code %d\n", rc);exit(EXIT_FAILURE);}
}void onConnectFailure(void* context, MQTTAsync_failureData* response)
{printf("Connect failed, rc %d\n", response ? response->code : 0);finished = 1;
}void onConnect(void* context, MQTTAsync_successData* response)
{MQTTAsync client = (MQTTAsync)context;MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;MQTTAsync_message pubmsg = MQTTAsync_message_initializer;int rc;printf("Successful connection\n");opts.onSuccess = onSend;opts.onFailure = onSendFailure;opts.context = client;pubmsg.payload = PAYLOAD;pubmsg.payloadlen = (int)strlen(PAYLOAD);pubmsg.qos = QOS;pubmsg.retained = 0;if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS){printf("Failed to start sendMessage, return code %d\n", rc);exit(EXIT_FAILURE);}
}int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
{/* not expecting any messages */return 1;
}int main(int argc, char* argv[])
{MQTTAsync client;MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;int rc;const char* uri = (argc > 1) ? argv[1] : ADDRESS;printf("Using server at %s\n", uri);if ((rc = MQTTAsync_create(&client, uri, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS){printf("Failed to create client object, return code %d\n", rc);exit(EXIT_FAILURE);}if ((rc = MQTTAsync_setCallbacks(client, client, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS){printf("Failed to set callback, return code %d\n", rc);exit(EXIT_FAILURE);}conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;conn_opts.onSuccess = onConnect;conn_opts.onFailure = onConnectFailure;conn_opts.context = client;if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS){printf("Failed to start connect, return code %d\n", rc);exit(EXIT_FAILURE);}printf("Waiting for publication of %s\n""on topic %s for client with ClientID: %s\n",PAYLOAD, TOPIC, CLIENTID);while (!finished)#if defined(_WIN32)Sleep(100);#elseusleep(10000L);#endifMQTTAsync_destroy(&client);return rc;
}
交叉编译:
arm-linux-gnueabihf-gcc MQTTAsync_publish.c -I /path/to/paho.mqtt.c-1.3.14/arm_install/include/ -L /path/to/paho.mqtt.c-1.3.14/arm_install/lib/ -l paho-mqtt3a -o MQTTAsync_publish
在arm开发板中运行:
/customer/mqtt_test # ./MQTTAsync_publish
Using server at 192.168.33.1:1883
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection
五、FAQ
Q:install TARGETS given target "paho-mqtt3a" which does not exist.
CMake error while compiling paho.mqtt.cpp · Issue #493 · eclipse-paho/paho.mqtt.cpp
-- Paho C: Bundled
CMake Error at CMakeLists.txt:75 (add_subdirectory):The source directory/path/to/paho.mqtt.cpp-1.5.2/externals/paho-mqtt-cdoes not contain a CMakeLists.txt file.CMake Error at CMakeLists.txt:79 (add_library):add_library cannot create ALIAS target "eclipse-paho-mqtt-c::paho-mqtt3a"because target "paho-mqtt3a" does not already exist.CMake Error at CMakeLists.txt:82 (add_library):add_library cannot create ALIAS target "eclipse-paho-mqtt-c::paho-mqtt3as"because target "paho-mqtt3as" does not already exist.CMake Error at CMakeLists.txt:97 (install):install TARGETS given target "paho-mqtt3a" which does not exist.
错误原因:没有下载 externals/paho-mqtt-c
。
解决方法:手动下载对应版本的 paho-mqtt-c
。
git clone https://github.com/eclipse-paho/paho.mqtt.c.git
mv paho.mqtt.c paho-mqtt-c
yoyo@yoyo:~/360Downloads/paho.mqtt.cpp-1.5.2$ tree -L 2 ./externals/
./externals/
└── paho-mqtt-c├── about.html├── android├── appveyor.yml├── build.xml├── cbuild.bat├── cmake├── cmake-build.sh├── CMakeLists.txt├── CODE_OF_CONDUCT.md├── CONTRIBUTING.md├── deploy_rsa.enc├── dist├── doc├── docs├── edl-v10├── epl-v20├── LICENSE├── Makefile├── NOTICE├── notice.html├── PULL_REQUEST_TEMPLATE.md├── README.md├── SECURITY.md├── src├── test├── test_package├── version.major├── version.minor└── version.patch
Q:libanl.so.1: cannot open shared object file: No such file or directory
./async_publish: error while loading shared libraries: libanl.so.1: cannot open shared object file: No such file or directory
错误原因:缺少libanl.so
库。
解决方法:
(1)在宿主机中查找 libanl.so
(通常在交叉编译工具链的lib目录中)。
find / -name "libanl.so.1" 2>/dev/null
yoyo@yoyo:/media/sda3/share/tftpboot/mqtt_test$ find / -name "libanl.so*" 2>/dev/null
/path/to/toolchains/arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/libanl.so.1
/path/to/toolchains/arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/libanl.so
yoyo@yoyo:/media/sda3/share/tftpboot/mqtt_test$ ll /path/to/toolchains/arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/libanl.so.1
lrwxrwxrwx 1 yoyo yoyo 14 Jun 8 2021 /path/to/toolchains/arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/libanl.so.1 -> libanl-2.33.so*
由此可见,libanl.so.1
是指向 libanl-2.33.so
的软链接。
(2)将 libanl-2.33.so
拷贝到arm开发板中,并创建软链接。
# 拷贝到arm开发板中
scp /path/to/toolchains/arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/libanl-2.33.so /lib/# 创建软链接
ln /lib/libanl-2.33.so /lib/libanl.so.1
Q:Connection attempt failed
/customer/mqtt_test # ./async_subscribe
Connecting to the MQTT server '192.168.33.1:1883'...Connection attempt failed
Connection attempt failed
Connection attempt failed
Connection attempt failed
错误原因:服务端mosquitto未启动。
解决方法:启动mosquitto服务端。
mosquitto -v -c /etc/mosquitto/mosquitto.conf# 后台启动
mosquitto -v -c /etc/mosquitto/mosquitto.conf -d
Q:MQTT error [-1]: TCP/TLS connect failure
/customer/mqtt_test # ./async_publish
Initializing for server '192.168.33.1:1883'......OKConnecting...
Waiting for the connection...
MQTT error [-1]: TCP/TLS connect failure
解决方法,同【Connection attempt failed
】。
相关文章:
交叉编译paho.mqtt.c和paho.mqtt.cpp(MQTT客户端)
一、参考资料 【MQTT】paho.mqtt.cpp 库的 介绍、下载、交叉编译、MQTT客户端例子源码-CSDN博客 【MQTT】paho.mqtt.c 库的“介绍、下载、交叉编译” 详解,以及编写MQTT客户端例子源码-CSDN博客 二、准备工作 1. 重要说明 paho.mqtt.cpp与paho.mqtt.cÿ…...
Prometheus中部署Alertmanager
部署Alertmanager 是 Prometheus 生态系统中的一个重要步骤,用于管理和处理 Prometheus生成的告警。Alertmanager和Prometheus Server一样均采用Golang实现,并且没有第三方依赖。一般来说我们可以通过以下几种方式来部署Alertmanager:二进制包…...
van-field组件设置为textarea属性被软键盘遮挡问题
在移动端van-field 输入框当type为text时,调出软键盘输入框会被顶上去,但type为textarea时不会被顶上去,可以用下面方法来实现: 1. 来2个van-field type为text的输入框z-index: 1 type为textarea的输入框z-index: 9999&#x…...
websheet之 编辑器
一、默认编辑器 该单元格编辑器是控件自带的编辑器,用户不需要指定。 二、下拉选择 该单元格编辑器是控件自带的编辑器的一种。该控件需要你指定下拉的数据源。在下面的例子中,我们给C3和C6单元格指定了币种的下拉选择编辑器。参数见:六、 参…...
氢气泄漏应急预案应包括哪些内容?
氢气泄漏应急预案是科研实验室中应对氢气泄漏事故的重要文件,其内容需要全面覆盖预防、检测、响应和善后处理等环节,确保在紧急情况下能够快速、有序地采取措施,最大限度地减少事故风险和损失。以下是氢气泄漏应急预案应包括的主要内容&#…...
【每天一个知识点】IPv4(互联网协议版本4)和IPv6(互联网协议版本6)
IPv4(互联网协议版本4)和IPv6(互联网协议版本6)是用于在互联网上标识和定位设备的两种主要协议。它们的主要区别在于地址空间、结构、以及一些附加功能。以下是两者的对比: 1. 地址长度 IPv4: 地址长度为32位…...
【高频考点精讲】前端构建工具对比:Webpack、Vite、Rollup和Parcel
前端构建工具大乱斗:Webpack、Vite、Rollup和Parcel谁是你的菜? 【初级】前端开发工程师面试100题(一) 【初级】前端开发工程师面试100题(二) 【初级】前端开发工程师的面试100题(速记版) 最近在后台收到不少同学提问:“老李啊,现在前端构建工具这么多,我该选哪个?…...
牛客小白月赛115-B题:签到题
题目传送门牛客网竞赛题目 一、题目描述 给定n道题目,每道题难度为aᵢ。要从中选出m道题组成比赛,使得难度最低的题目(签到题)数量尽可能多。求签到题的最大可能数量。 输入: 第一行两个整数n,m(1≤m≤n≤210⁵)第…...
【QQMusic项目复习笔记——音乐管理模块详解】第四章
🌹 作者: 云小逸 🤟 个人主页: 云小逸的主页 🤟 motto: 要敢于一个人默默的面对自己,强大自己才是核心。不要等到什么都没有了,才下定决心去做。种一颗树,最好的时间是十年前,其次就是现在&…...
IPv6 技术细节 | 源 IP 地址选择 / Anycast / 地址自动配置 / 地址聚类分配
注:本文为 “IPv6 技术细节” 相关文章合集。 部分文章中提到的其他文章,一并引入。 略作重排,未整理去重。 如有内容异常,请看原文。 闲谈 IPv6 - 典型特征的一些技术细节 iteye_21199 于 2012-11-10 20:54:00 发布 0. 巨大的…...
代码随想录算法训练营day11(二叉树)
华子目录 翻转二叉树思路 对称二叉树思路 二叉树的最大深度思路 翻转二叉树 https://leetcode.cn/problems/invert-binary-tree/description/ 思路 采用递归的思路可以前序遍历和后序遍历,不能使用中序遍历 # Definition for a binary tree node. # class TreeNo…...
A Comprehensive Survey of Spoken Language Models
语音大语言模型(Spoken Language Model, SLM)正在引领人工智能领域的新一轮革新浪潮。正如文本自然语言处理从任务特定模型迈向通用大语言模型的演进,语音领域也正在经历类似转型。 为填补该领域系统性综述的空白,芝加哥大学、卡…...
深入解析 SMB 相关命令:smbmap、smbclient、netexec 等工具的使用指南
Server Message Block(SMB)协议是广泛应用于文件共享、打印机共享和进程间通信的网络协议,尤其在 Windows 环境中常见。渗透测试和网络安全审计中,SMB 是一个重要的攻击面,相关工具如 smbmap、smbclient 和 netexec 提…...
伊克罗德信息亮相亚马逊云科技合作伙伴峰会,以ECRobot 智能云迁移助手在GenAI Tech Game比赛勇夺金牌!
十年同行,共赴盛会:伊克罗德信息亮相2025亚马逊云科技Partner Summit 2025亚马逊云科技合作伙伴峰会(AWS Partner Summit)于乌镇盛大启幕,这场全球云计算领域的顶级盛会汇聚了亚马逊云科技全球核心合作伙伴、行业领袖与…...
【蓝桥杯】P12165 [蓝桥杯 2025 省 C/Java A] 最短距离
最短距离 题目描述 在一条一维的直线上,存在着 n n n 台显示器和 n n n 个电源插座。老师给小蓝布置了个任务:负责将每台显示器通过电源线与一个插座相连接(每个插座最多只能给一台显示器供电);同时,老…...
深入浅出Sentinel:分布式系统的流量防卫兵
引言 在当今的微服务架构和分布式系统中,服务间的依赖关系错综复杂,一个服务的故障可能会像多米诺骨牌一样引发整个系统的崩溃。如何有效地保护系统免受突发流量、不稳定依赖服务的影响,成为每个架构师和开发者必须面对的挑战。今天…...
vite+vue2+elementui构建之 vite.config.js
webpack版本太低,构建依赖太多,头大。 各种查阅资料,弄了一份直通构建vite构建elementUi核心文件, 构建基于开源若依vue2vue3版本改造,感谢开源,感谢若依。 package.json 地址 vitevue2elementui构建之…...
【Pandas】pandas DataFrame radd
Pandas2.2 DataFrame Binary operator functions 方法描述DataFrame.add(other)用于执行 DataFrame 与另一个对象(如 DataFrame、Series 或标量)的逐元素加法操作DataFrame.add(other[, axis, level, fill_value])用于执行 DataFrame 与另一个对象&…...
Java 后端开发环境安装
Java环境安装 1. 安装程序 Java1.8下载,由于官网下载需要登录,比较麻烦,所以我将安装文件放到了我的资源中,大家通过资源直接下载即可 jdk-8u351-windows-x64.exe 大家根据自己的电脑的配置选择适当的版本 然后一路下一步 这里…...
Azure Data Factory ETL设计与调度最佳实践
一、引言 在Azure Data Factory (ADF) 中,调度和设计ETL(抽取、转换、加载)过程需要综合考量多方面因素,以确保数据处理高效、可扩展、可靠且易于维护。以下将详细介绍相关关键考虑因素、最佳实践,并辅以具体示例说明…...
【Mybatis】MyBatisPlus的saveBatch真的是批量插入吗?深度解析与性能优化
前言 在使用MyBatis-Plus进行批量数据插入时,许多开发者会发现:即使调用saveBatch方法,数据库仍会产生大量INSERT语句。本文将深入源码揭示背后的真相,并提供3种性能优化方案,让你的批量插入速度提升10倍!…...
图像预处理-图像亮度变换
一.亮度变换 首先有两个关联的说法: 亮度调整:像素强度整体变高或者变低。 对比度调整:暗处像素强度变低,亮处像素强度变高,从而拉大中间某个区域范围的显示精度。 opencv中操作这两种变换的公式为: 对比…...
基于AI应用创业IDEA:使用百度搜索开放平台的MCP广场智能推荐MCPServices服务
基于AI应用创业IDEA:使用百度搜索开放平台的MCP广场智能推荐MCPServices服务 在当今快速发展的技术时代,人工智能(AI)已经成为推动各行各业创新的关键力量。特别是在创业领域,AI技术不仅能够帮助提升产品性能…...
URP-利用矩阵在Shader中实现物体的平移和缩放
一、平移 方法一: v.positionOS.xyz _Translate.xyz; 方法二: 利用矩阵实现平移: 二、缩放 方法一: v.positionOS.xyz * _Scale.xyz*_Scale.w; _Scale.w实现全局缩放 方法二: Shader"unity/Translation"…...
跟着尚硅谷学vue-day5
计算属性和watch监视 一.姓名案例 1.姓名案例-插值语法 <div id"root">姓:<input type"text" value"张" v-model"firstname"><br/><br/>名:<input type"text" value&q…...
猫咪如厕检测与分类识别系统系列【十三】猫咪进出事件逻辑及日志优化【下】
前情提要 家里养了三只猫咪,其中一只布偶猫经常出入厕所。但因为平时忙于学业,没法时刻关注牠的行为。我知道猫咪的如厕频率和时长与健康状况密切相关,频繁如厕可能是泌尿问题,停留过久也可能是便秘或不适。为了更科学地了解牠的如…...
【Java学习笔记】冒泡排序
冒泡排序 思想:经过一轮遍历比较,把最大的放在数组的末尾 int[] a {3, 2, 1}; for( int i 0; i < a.length-1; i){for( int j 0; j < a.length-1-i; j){if(a[j] > a[j1]){int temp a[j];a[j] a[j1];a[j1] temp;}} } for( int i 0; i &…...
【含文档+PPT+源码】基于微信小程序的校园快递平台
项目介绍 本课程演示的是一款基于微信小程序的校园快递平台,主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的 Java 学习者。 1.包含:项目源码、项目文档、数据库脚本、软件工具等所有资料 2.带你从零开始部署运行本套系统 3.该项目附带…...
Vue3 自定义指令完全指南
Vue3 自定义指令完全指南 目录 基本概念指令注册方式常用应用场景注意事项 基本概念 在Vue3中,自定义指令是用于直接操作DOM的重要工具。相比Vue2,Vue3的指令系统进行了优化和简化。 生命周期钩子 钩子名称对应Vue2名称触发时机createdbind元素属性…...
神经辐射场(NeRF)技术解析:3D重建与虚拟世界的未来
神经辐射场(NeRF)技术解析:3D重建与虚拟世界的未来 ——从算法突破到元宇宙基础设施的演进之路 摘要 本文通过算法演进图谱、训练流程解析、PyTorch代码实战及产业应用洞察,构建从学术创新到工程落地的完整技术框架。实验数据显…...
050_基于springboot的音乐网站
一、系统架构 前端:vue | element-ui | html | jquery | css | ajax 后端:springboot | mybatis 环境:jdk1.8 | mysql | maven | nodejs | idea 二、代码及数据 三、功能介绍 01. web端-注册 02. web端-登录 03. web…...
Django之旅:第七节--模版继承
定义母版—new.html <!DOCTYPE html> <html lang"en"><head></head><body><div>{% block contents %}{% endblock %}</div></body> </html> 继承母模板 {% extends new.html %} {% block contents %}&…...
Windows 10 上运行 Ollama 时遇到 llama runner process has terminated: exit status 2
在 Windows 10 上运行 Ollama 时遇到 llama runner process has terminated: exit status 2 错误,可能是由多种原因引起的。以下是逐步解决方案: 1. 检查 Ollama 服务状态 按 Win R 输入 services.msc,找到 Ollama 服务,确保其状…...
基于 Python(selenium) 的百度新闻定向爬虫:根据输入的关键词在百度新闻上进行搜索,并爬取新闻详情页的内容
该项目能够根据输入的关键词在百度新闻上进行搜索,并爬取新闻详情页的内容。 一、项目准备 1. 开发环境配置 操作系统:支持 Windows、macOS、Linux 等主流操作系统,本文以 Windows 为例进行说明。Python 版本:建议使用 Python 3.8 及以上版本,以确保代码的兼容性和性能。…...
YOLOX-PAI手部检测模型
YOLOX-PAI手部检测模型 # numpy > 1.20 from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasksmodel_id iic/cv_yolox-pai_hand-detection hand_detection pipeline(Tasks.domain_specific_object_detection, modelmodel_id) output …...
速成GO访问sql,个人笔记
更多个人笔记:(仅供参考,非盈利) gitee: https://gitee.com/harryhack/it_note github: https://github.com/ZHLOVEYY/IT_note 本文是基于原生的库 database/sql进行初步学习 基于ORM等更多操作可以关注我…...
SkyWalking 安装与使用详细总结
SkyWalking 是一款开源的分布式应用性能监控(APM)系统,用于监控微服务、云原生应用、容器等。它支持服务跟踪、性能监控、日志分析和数据可视化。以下是 SkyWalking 的安装与使用详细步骤。 一、SkyWalking 安装 1. 下载 SkyWalking 访问 …...
产业观察:中国商飞2025.4.25
一.中国商飞简介 1.1 公司背景 中国商飞于 2008年5月11日 在上海成立,是实施国家大型飞机重大专项中大型客机项目的主体。公司由多家国有大型企业共同出资组建,包括国务院国资委、上海国盛集团、中航工业集团等。总部设在上海,拥有多个研发…...
【爬虫】DrissionPage-获取douyim用户下的视频
之前看过DrissionPage,觉得很厉害,比selenium简单,适合新手。因为盲目跟风逆向,今天看了一个DrissionPage案例直播,学习一下,真香哈。 DrissionPage官网:🛰️ 概述 | DrissionPage官…...
Kubernetes 节点 Not Ready 时 Pod 驱逐机制深度解析(下)
#作者:邓伟 文章目录 三、深度解析:源码逻辑与调优策略四、常见问题与排查五、最新动态与技术演进总结 三、深度解析:源码逻辑与调优策略 TaintManager 核心源码逻辑 (1)参数定义(kube-controller-manage…...
markdown自动标题序号,标题序号,目录处理
在vscode下有插件 markdown aheads markdown标题与目录处理插件。 使用方法:在vscode中 ctrlshiftp 输入 AHeads 选择功能。 包含以下功能: "AHeads:AddIndex": "添加标题序号" "AHeads:RemoveIndex": "移除标题…...
模式设计简介
设计模式简介 设计模式是软件开发中经过验证的最佳实践解决方案,它是针对特定问题的通用解决方案,能够帮助开发者提升代码的可维护性、可扩展性和复用性。设计模式并非具体的代码实现,而是一种解决问题的思路和方法论,它源于大量的实践经验总结,旨在解决软件开发过程中反…...
Linux字符设备驱动开发的详细步骤
1. 确定主设备号 手动指定:明确设备号时,使用register_chrdev_region()静态申请(需确保未被占用)。动态分配:通过alloc_chrdev_region()由内核自动分配主设备号(更灵活,推…...
融合注意力机制和BiGRU的电力领域发电量预测项目研究,并给出相关代码
大家好,我是微学AI,今天给大家介绍一下融合注意力机制和BiGRU的电力领域发电量预测项目研究,并给出相关代码。 文章目录 一、项目背景二、融合注意力机制和BiGRU的技术原理(一)双向门控循环单元(BiGRU&…...
基于 Netmiko 的网络设备自动化操作
学习目标 掌握 Netmiko 库的核心功能与使用场景。能够通过 Netmiko 连接多厂商设备并执行命令和配置。实现批量设备管理、配置备份与自动化巡检。掌握异常处理、日志记录与性能优化技巧。理解 Netmiko 在自动化运维体系中的角色。 1. Netmiko 简介 1.1 什么是 Netmiko Netmi…...
eclipse怎么导入junit4
JUnit 4 是 Java 编程语言里常用的单元测试框架,在 Eclipse 中导入 JUnit 4 可按以下步骤操作: 1. 新建或打开 Java 项目 如果你已经有一个 Java 项目,可直接打开它;若没有,就新建一个 Java 项目。操作步骤如下&…...
深入理解C语言函数之模拟实现strcpy()strcat()
文章目录 前言一、strcpy的模拟实现二、strcat的模拟实现总结 前言 前面我们用三种方法模拟实现了一下strlen,所以这篇文章模拟实现以下strcpy()strcat() 一、strcpy的模拟实现 首先我们去官网找到strcpy的用法和原…...
[mysql]窗口函数
目录 窗口函数: 为何要学习窗口函数,与mysql5.7实现语句对比 现在我们介绍一下窗口函数: 函数规则 1序号函数 2分布函数 3前后函数 5其他函数 总结 窗口函数: 首先数据库的迁移是非常慢的,大家学习新特性的时候要考虑自己公司的数据库版本是不是和自己学习的吻合 为何…...
IT人力外包定义-优势-服务流程介绍
IT人力外包:企业灵活用工的“技术人才蓄水池” 在数字化转型的浪潮中,企业对IT人才的需求日益增长,但招聘、培养和管理技术团队的成本和风险也随之攀升。此时,IT人力外包应运而生,成为企业高效获取技术资源的解决方案…...
Cancer Cell|scRNA-seq + scTCR + 空间多组学整合分析,揭示CD8⁺ T细胞在免疫治疗中的“双路径” | 临床问题的组学解答
Cancer Cell|scRNA-seq scTCR 空间多组学整合分析,揭示CD8⁺ T细胞在免疫治疗中的“双路径” 👋 欢迎关注我的生信学习专栏~ 如果觉得文章有帮助,别忘了点赞、关注、评论,一起学习 近日,《Cancer Cell》…...