一、下载:
sudo wget https://mosquitto.org/files/source/mosquitto-2.0.15.tar.gz
tar -zxvf mosquitto-2.0.15.tar.gz //编译是cd进入源码文件夹内
二、安装:
安装依赖:
sudo apt install gcc g++ make libssl-dev libcjson-dev libwebsockets-dev
编译安装:
sudo make
sudo make install
三、查看头文件和静态库安装路径:
头文件一般在:/usr/local/include/路径下;
静态库(.so文件):/usr/local/lib路径下;
四.添加到ROS2环境中,CmakeList.txt文件eg:
cmake_minimum_required(VERSION 3.8)
project(mqtt_conn)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED) # 添加geometry_msgs包
# 1.添加MQTT支持
# 查找Mosquitto库 - 使用find_library确保正确链接
find_library(MOSQUITTO_C_LIBRARY NAMES mosquitto)
find_library(MOSQUITTO_CPP_LIBRARY NAMES mosquittopp)
if(NOT MOSQUITTO_C_LIBRARY OR NOT MOSQUITTO_CPP_LIBRARY)
message(FATAL_ERROR "Mosquitto libraries not found. Please install libmosquitto-dev and libmosquittopp-dev.")
endif()
add_executable(publish_node src/publish_node.cpp)
target_include_directories(publish_node PUBLIC include)
ament_target_dependencies(publish_node rclcpp std_msgs geometry_msgs)
# 包含头文件
target_include_directories(publish_node PUBLIC
${CMAKE_INCLUDE_PATH}
)
# 2.直接链接Mosquitto库
target_link_libraries(publish_node
${rclcpp_LIBRARIES}
${std_msgs_LIBRARIES}
${geometry_msgs_LIBRARIES}
${MOSQUITTO_C_LIBRARY}
${MOSQUITTO_CPP_LIBRARY}
)
install(TARGETS
publish_node
DESTINATION lib/${PROJECT_NAME}
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
通过以上步骤,可以把mqtt协议集成到ros2中。
8481

被折叠的 条评论
为什么被折叠?



