带有 ROS Jade 未定义参考的 Mosquitto

Mosquitto with ROS Jade undefined reference

我目前在 Ubuntu 14.04 中使用 ROS Jade,并且正在尝试为我的 Mosquitto 软件创建一个 MQTT 发布器。但是,我无法通过 catkin_make 正确构建它。在我的主要 cpp 代码中,我包含了 "mosquitto.h" 文件。抱歉,我无法 post 我的 cpp 文件,因为它是用于工作目的。

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)

catkin_package(
INCLUDE_DIRS include
LIBRARIES mqtt 
CATKIN_DEPENDS roscpp std_msgs
DEPENDS system_lib
)

set(MOSQ_LIB_LOCATIONS
/usr/lib
)

set(INCLUDE_HEADER_FILES
src/mosquitto.h
#include/lib/cpp/mosquittopp.h
)

include_directories(
${catkin_INCLUDE_DIRS}
${mosquitto_INCLUDE_DIRS}
/home/catkin_ws/src/mqtt/include/lib
/home/catkin_ws/src/mqtt/include/lib/cpp
)

add_executable(mqtt src/mqtt.transmit.cpp ${INCLUDE_HEADER_FILES} ${MOSQ_LIB_LOCATIONS})
target_link_libraries(mqtt ${catkin_LIBRARIES})

错误

mqtt.transmit.cpp:(.text+0x1f8): undefined reference to `mosquitto_lib_init'
mqtt.transmit.cpp:(.text+0x210): undefined reference to `mosquitto_new'
mqtt.transmit.cpp:(.text+0x237): undefined reference to `mosquitto_username_pw_set'
mqtt.transmit.cpp:(.text+0x259): undefined reference to `mosquitto_connect'
mqtt.transmit.cpp:(.text+0x285): undefined reference to `mosquitto_loop_start'
mqtt.transmit.cpp:(.text+0x2bc): undefined reference to `mosquitto_publish'
mqtt.transmit.cpp:(.text+0x2d0): undefined reference to `mosquitto_loop_stop'
mqtt.transmit.cpp:(.text+0x2df): undefined reference to `mosquitto_disconnect'
mqtt.transmit.cpp:(.text+0x2ee): undefined reference to `mosquitto_destroy'
mqtt.transmit.cpp:(.text+0x2f3): undefined reference to `mosquitto_lib_cleanup'
collect2: error: ld returned 1 exit status

我最初以为我包含的头文件读取错误,但如果是这样,就会出现"no such file directory"错误。 "undefined references" 中的函数位于我包含的头文件中,不确定为什么它仍然未定义。将不胜感激摆脱未定义参考的指导。

非常感谢!

[编辑] 我从以下站点获得了 MQTT 发布 cpp 代码,您必须滚动到底部。谢谢! Mosquitto 软件运行良好。 https://robomq.readthedocs.io/en/latest/MQTT/

在这种情况下,是链接器找不到包含错误输出中显示的函数名称的目标文件。除非您更改了此 CMake 文件,否则我猜想必要的目标文件不在您的 /usr/lib 目录中,链接器正在寻找该目录。

已解决。使用 Mosquitto 时,我必须 link 我的 CMakeList 中的客户端库。基本上是 libmosquitto.so 文件,它是客户端库。

我将以下内容添加到我的 cmake 列表中:

set(Mosquitto_libs
  /usr/lib/x86_64-linux-gnu/libmosquitto.so
  /usr/lib/x86_64-linux-gnu/libmosquitto.so.1
)
target_link_libraries(mqtt_pub_node ${catkin_LIBRARIES} ${Mosquitto_libs})