cpp mqtt paho 使用,在Linux上使用cmake独立编译PahoMqttCpp示例

本文档描述了作者在Linux环境下使用CMake独立编译Paho MQTT C++库示例async_subscribe.cpp时遇到的问题及解决过程。在编译过程中,遇到了链接错误和找不到PahoMqttC包的问题。通过更新CMakeLists.txt文件,特别是正确引用PahoMqttCpp库的导入目标,最终解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

My goal

... is to use the sample code async_subscribe.cpp from the PahoMqttCpp project (https://github.com/eclipse/paho.mqtt.cpp) as a standalone application to then modify it for my needs.

My approach

Preliminaries

On the newest model of Raspberry pi (uname -a--> 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux) I have followed the description in the PahoMqttCpp README.md file. I compiled successfully the C-library (v1.2.1) and then compiled successfuly the Cpp part, finally installed both libraries to /usr/local/lib. During this process, no errors occurred.

My project

Then, as a start for my own project, I copied the async_subscribe.cpp from the PahoMqttCpp samples into a empty directory and started to build a CMakeLists.txt to compile it. Naively, I started with this version:

cmake_minimum_required(VERSION 3.5)

add_library(PahoMqttC SHARED IMPORTED)

set_target_properties(PahoMqttC PROPERTIES IMPORTED_LOCATION /usr/share/lib/libpaho-mqtt3a.so)

set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package(Threads REQUIRED)

find_package(PahoMqttCpp REQUIRED)

add_executable(async_subscribe async_subscribe.cpp)

include_directories(${PahoMqttCpp_INCLUDE_DIRS})

target_link_libraries(async_subscribe ${PahoMqttCpp_LIBRARIES})

target_link_libraries(async_subscribe ${PahoMqttC_LIBRARIES})

install(TARGETS async_subscribe RUNTIME DESTINATION bin)

The following cmake -Bbuild -H. command showed no errors, the output was

pi@homepi:~/Develop/CppMosquitto/example $ cmake -Bbuild -H.

-- The C compiler identification

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值