ROS2 CMakeLists.txt 和 package.xml

news/2025/1/23 3:53:56/

这里记录一下ROS2中功能包package.xml和CMakeLists.txt的格式。以LIO-SAM的ROS2版本为例:

一:CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(lio_sam)if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)set(CMAKE_BUILD_TYPE Release)
endif()# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclpy REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(pcl_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(tf2_sensor_msgs REQUIRED)
find_package(tf2_eigen REQUIRED)
find_package(tf2_ros REQUIRED)
set(OpenCV_STATIC ON)
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED)
find_package(GTSAM REQUIRED)
find_package(Eigen REQUIRED)
find_package(OpenMP REQUIRED)include_directories(include/lio_sam
)rosidl_generate_interfaces(${PROJECT_NAME} "msg/CloudInfo.msg" "srv/SaveMap.srv" DEPENDENCIES std_msgs sensor_msgs)add_executable(${PROJECT_NAME}_featureExtraction src/featureExtraction.cpp)
ament_target_dependencies(${PROJECT_NAME}_featureExtraction rclcpp rclpy std_msgs sensor_msgs geometry_msgs nav_msgs pcl_msgs visualization_msgs tf2 tf2_ros tf2_eigen tf2_sensor_msgs tf2_geometry_msgs OpenCV PCL)
rosidl_get_typesupport_target(cpp_typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(${PROJECT_NAME}_featureExtraction "${cpp_typesupport_target}") add_executable(${PROJECT_NAME}_imageProjection src/imageProjection.cpp)
ament_target_dependencies(${PROJECT_NAME}_imageProjection rclcpp rclpy std_msgs sensor_msgs geometry_msgs nav_msgs pcl_msgs visualization_msgs pcl_msgs tf2 tf2_ros tf2_eigen tf2_sensor_msgs tf2_geometry_msgs OpenCV PCL)
target_link_libraries(${PROJECT_NAME}_imageProjection "${cpp_typesupport_target}") add_executable(${PROJECT_NAME}_imuPreintegration src/imuPreintegration.cpp)
ament_target_dependencies(${PROJECT_NAME}_imuPreintegration rclcpp rclpy std_msgs sensor_msgs geometry_msgs nav_msgs pcl_msgs visualization_msgs tf2 tf2_ros tf2_eigen tf2_sensor_msgs tf2_geometry_msgs OpenCV PCL GTSAM Eigen)
target_link_libraries(${PROJECT_NAME}_imuPreintegration gtsam "${cpp_typesupport_target}")add_executable(${PROJECT_NAME}_mapOptimization src/mapOptmization.cpp)
ament_target_dependencies(${PROJECT_NAME}_mapOptimization rclcpp rclpy std_msgs sensor_msgs geometry_msgs nav_msgs pcl_msgs visualization_msgs tf2 tf2_ros tf2_eigen tf2_sensor_msgs tf2_geometry_msgs OpenCV PCL GTSAM)
if (OpenMP_CXX_FOUND)target_link_libraries(${PROJECT_NAME}_mapOptimization gtsam "${cpp_typesupport_target}" OpenMP::OpenMP_CXX)
else()target_link_libraries(${PROJECT_NAME}_mapOptimization gtsam "${cpp_typesupport_target}")
endif()install(DIRECTORY launchDESTINATION share/${PROJECT_NAME}/
)install(DIRECTORY configDESTINATION share/${PROJECT_NAME}/
)install(TARGETS ${PROJECT_NAME}_imageProjectionDESTINATION lib/${PROJECT_NAME}
)install(TARGETS ${PROJECT_NAME}_imuPreintegrationDESTINATION lib/${PROJECT_NAME}
)install(TARGETS ${PROJECT_NAME}_featureExtractionDESTINATION lib/${PROJECT_NAME}
)install(TARGETS ${PROJECT_NAME}_mapOptimizationDESTINATION lib/${PROJECT_NAME}
)install(DIRECTORY "include/"DESTINATION include
)ament_export_include_directories(include)if(BUILD_TESTING)find_package(ament_lint_auto REQUIRED)# the following line skips the linter which checks for copyrights# uncomment the line when a copyright and license is not present in all source files#set(ament_cmake_copyright_FOUND TRUE)# the following line skips cpplint (only works in a git repo)# uncomment the line when this package is not in a git repo#set(ament_cmake_cpplint_FOUND TRUE)ament_lint_auto_find_test_dependencies()
endif()ament_package()

其中:

install 

install(TARGETS ${PROJECT_NAME}_mapOptimizationDESTINATION lib/${PROJECT_NAME}
)

是将可执行程序 ${PROJECT_NAME}_mapOptimization安装在./install/${PROJECT_NAME}/lib/${PROJECT_NAME}/目录下。

放在别的路径下,启动ros2节点的时候会提示找不到。

ament_target_dependencies

  • ament_target_dependencies用于指定ROS2目标级别(target,例如可执行文件或库的依赖项,包括其他ROS2软件包和库。这个命令会告诉构建系统在构建和运行过程中自动处理这些依赖项,并确保正确地解析和包含所需的头文件。
  • target_link_libraries用于指定目标(例如可执行文件或库)所依赖的库。这通常用于非ROS软件包的依赖项,如OpenCV、Boost等。您可以使用它来链接和引入这些库文件。

ament_export_

cmake_minimum_required(VERSION 3.5)
project(slam)if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)set(CMAKE_BUILD_TYPE Release)
endif ()set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)set(CMAKE_CXX_FLAGS_RELEASE "-O2")add_subdirectory(mapping)
add_subdirectory(map_manager)# slam是一个总包,内部自己编译libs和binary,根目录下的cmake将编译好的内容导出并安装
ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME}.mapping)ament_package()

ament_export_dependencies命令将软件包(package)级别导出依赖项。

ament_export_libraries命令将软件包级别所依赖的库导出。

以上导出都是为了别的软件包依赖此软件包时,自动获取此软件包所导出的库和依赖项。

rosidl_generate_interfaces

rosidl_generate_interfaces是ROS 2中专门用于生成消息和服务定义文件的函数。它是由ROS 2提供的CMake函数,用于根据接口定义文件生成相应的ROS消息和服务文件。

在构建过程中调用。它接受以下参数:

  • package_name:生成的接口文件将属于的软件包名称。
  • interface_files:一个字符串列表,指定接口定义文件的路径。可以包含消息文件(.msg)和服务文件(.srv)。
  • DEPENDENCIES:一个可选的参数,用于指定接口定义文件中使用的其他ROS消息或服务的依赖项。

需要在packag.xml中添加下面这行命令。

<member_of_group>rosidl_interface_packages</member_of_group> #添加这一行

编译完成后在${PROJECT_NAME}_ws/install/${PROJECT_NAME}/include下你应该可以看到C++的头文件。

二:package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3"><name>lio_sam</name><version>1.0.0</version><description>Lidar Odometry</description><maintainer email="shant@mit.edu">Tixiao Shan</maintainer><license>TODO</license><author>Tixiao Shan</author><buildtool_depend>ament_cmake</buildtool_depend><buildtool_depend>rosidl_default_generators</buildtool_depend><build_depend>rclcpp</build_depend><build_depend>rclpy</build_depend><build_depend>std_msgs</build_depend><build_depend>sensor_msgs</build_depend><build_depend>nav_msgs</build_depend><build_depend>geometry_msgs</build_depend><build_depend>tf2_geometry_msgs</build_depend><build_depend>tf2_sensor_msgs</build_depend><build_depend>visualization_msgs</build_depend><build_depend>pcl_msgs</build_depend><build_depend>tf2_eigen</build_depend><build_depend>tf2_ros</build_depend><build_depend>tf2</build_depend><build_depend>OpenCV</build_depend><build_depend>PCL</build_depend><build_depend>GTSAM</build_depend><build_depend>Eigen</build_depend><exec_depend>rosidl_default_runtime</exec_depend><exec_depend>rclcpp</exec_depend><exec_depend>rclpy</exec_depend><exec_depend>std_msgs</exec_depend><exec_depend>sensor_msgs</exec_depend><exec_depend>nav_msgs</exec_depend><exec_depend>geometry_msgs</exec_depend><exec_depend>visualization_msgs</exec_depend><exec_depend>pcl_msgs</exec_depend><exec_depend>tf2_geometry_msgs</exec_depend><exec_depend>tf2_sensor_msgs</exec_depend><exec_depend>tf2_eigen</exec_depend><exec_depend>tf2_ros</exec_depend><exec_depend>tf2</exec_depend><exec_depend>OpenCV</exec_depend><exec_depend>PCL</exec_depend><exec_depend>GTSAM</exec_depend><exec_depend>Eigen</exec_depend><member_of_group>rosidl_interface_packages</member_of_group><test_depend>ament_lint_auto</test_depend><test_depend>ament_lint_common</test_depend><export><build_type>ament_cmake</build_type></export>
</package>

其中package name应该和CMakeLists中的project name保持一致。

编译报错:

1.cmake_minimum_required

CMake Error at /opt/ros/iron/share/ament_cmake_core/cmake/core/list_append_unique.cmake:30 (if):if given arguments:"NOT" "element" "IN_LIST" "paths_to_search"Unknown arguments specified
Call Stack (most recent call first):/opt/ros/iron/share/ament_cmake_core/cmake/index/ament_index_get_resources.cmake:51 (list_append_unique)/opt/ros/iron/share/rosidl_core_generators/cmake/rosidl_core_generators-extras.cmake:5 (ament_index_get_resources)/opt/ros/iron/share/rosidl_core_generators/cmake/rosidl_core_generatorsConfig.cmake:41 (include)/opt/ros/iron/share/rosidl_default_generators/cmake/ament_cmake_export_dependencies-extras.cmake:21 (find_package)/opt/ros/iron/share/rosidl_default_generators/cmake/rosidl_default_generatorsConfig.cmake:41 (include)CMakeLists.txt:15 (find_package)

解决方法:cmake_minimum_required(VERSION 3.0.2) 改成cmake_minimum_required(VERSION 3.5)


http://www.ppmy.cn/news/1346895.html

相关文章

Python3 交叉编译 numpy pandas scipy scikit-learn

1. 概述 由于需要将Python3.7 和一些软件包交叉编译到 armv7 平台硬件&#xff0c;如果是arm64位的系统&#xff0c;很多包都有预编译好的版本&#xff0c;可直接下载。本文主要在基于 crossenv(https://github.com/benfogle/crossenv)环境下交叉编译。 2. 编译环境搭建 创建…

DataX源码分析 TaskGroupContainer

系列文章目录 一、DataX详解和架构介绍 二、DataX源码分析 JobContainer 三、DataX源码分析 TaskGroupContainer 四、DataX源码分析 TaskExecutor 五、DataX源码分析 reader 六、DataX源码分析 writer 七、DataX源码分析 Channel 文章目录 系列文章目录TaskGroupContainer初始…

es6中标签模板

之所以写这篇文章&#xff0c;是因为标签模板是一个很容易让人忽略的知识点 首先我们已经非常熟悉模板字符串的使用方法 const name "诸葛亮" const templateString hello, My name is ${name}标签模板介绍 这里的标签模板其实不是模板&#xff0c;而是函数调用…

面向对象的三大特征之一继承

继承 继承的特性 概念&#xff1a;可以使得子类具有父类的属性(成员变量)和方法(成员方法)&#xff0c;还可以在子类中重新定义&#xff0c;追加属性和方法。 继承的格式&#xff1a; public class 子类名 extends 父类名{} 父类&#xff1a;基类、超类 子类&#xff1a;派生…

小白水平理解面试经典题目LeetCode 71. Simplify Path【Stack类】

71. 简化路径 小白渣翻译 给定一个字符串 path &#xff0c;它是 Unix 风格文件系统中文件或目录的绝对路径&#xff08;以斜杠 ‘/’ 开头&#xff09;&#xff0c;将其转换为简化的规范路径。 在 Unix 风格的文件系统中&#xff0c;句点 ‘.’ 指的是当前目录&#xff0c;…

NLP深入学习:《A Survey of Large Language Models》详细学习(五)

文章目录 1. 前言2. Prompts2.1 Prompting2.1.1 Prompt 构建2.1.2 Prompt 优化 2.2 上下文学习2.2.1 ICL 公式化2.3 演示设计 2.3 思维链 Prompt2.3.1 改进的 CoT Prompt 策略2.3.2 深入讨论 2.4 解决复杂任务的规划方法2.4.1 规划生成2.4.2 反馈获取2.4.3 规划细化 3. 参考 1.…

jsp康养小镇管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 JSP康养小镇管理系统是一套完善的java web信息管理系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开发&#xff0c;数据库为Mysql5.0&a…

数据库管理-第147期 最强Oracle监控EMCC深入使用-04(20240207)

数据库管理147期 2024-02-07 数据库管理-第147期 最强Oracle监控EMCC深入使用-04&#xff08;20240207&#xff09;1 发现Exadata2 Exadata监控计算节点&#xff1a;存储节点RoCE交换机管理交换机PDU 总结 数据库管理-第147期 最强Oracle监控EMCC深入使用-04&#xff08;202402…