从源码编译安装mosquitto

一、下载:

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中。

源码编译mosquitto通常是指从Mosquitto项目的源代码仓库下载源码,然后按照项目指导进行编译安装。以下是大致步骤: 1. **获取源码**: - 首先访问Mosquitto的GitHub主页(https://github.com/eclipse/mosquitto),克隆或下载最新版本的源码库到本地目录,例如:`git clone https://github.com/eclipse/mosquitto.git` 2. **检查依赖**: - 确保本地环境中已经安装了必要的编译工具,比如C/C++编译器、Makefile支持、Git等。 - 对于特定的操作系统,可能还需要检查并安装其他依赖,比如libssl、zlib等。 3. **进入源码目录**: - 切换至刚克隆的mosquitto目录:`cd mosquitto` 4. **构建选项配置**: - 创建一个config.mk文件,编辑它来配置编译选项,包括是否启用SSL、TLS等特性,以及选择合适的编译器 flags。 5. **编译**: - 进入build目录(如果没有就创建):`mkdir build && cd build` - 使用make命令编译源码,例如:`cmake ..` 或 `cmake .` (取决于操作系统) - 接着执行构建过程:`make` 6. **安装**: - 如果希望将mosquitto安装到系统位置,可以使用`sudo make install`。如果没有特殊权限,可以选择安装到用户自定义目录。 7. **测试**: - 完成编译安装后,可以运行测试脚本来验证mosquitto是否正常工作:`make check` 8. **清理**: - 完成编译后,可以删除临时文件和构建目录,保持目录整洁:`make clean` 如果你在编译过程中遇到问题,记得查阅官方文档或社区论坛,查找相关的解决方法。同时,根据你的目标环境(Linux、Windows或MacOS)和具体需求,上述步骤可能需要适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值