本文介绍: ROS2 CMakeLists.txt 和 package.xml

这里记录一下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 launch
  DESTINATION share/${PROJECT_NAME}/
)

install(
  DIRECTORY config
  DESTINATION share/${PROJECT_NAME}/
)

install(
  TARGETS ${PROJECT_NAME}_imageProjection
  DESTINATION lib/${PROJECT_NAME}
)

install(
  TARGETS ${PROJECT_NAME}_imuPreintegration
  DESTINATION lib/${PROJECT_NAME}
)

install(
  TARGETS ${PROJECT_NAME}_featureExtraction
  DESTINATION lib/${PROJECT_NAME}
)

install(
  TARGETS ${PROJECT_NAME}_mapOptimization
  DESTINATION 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}_mapOptimization
  DESTINATION 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)

原文地址:https://blog.csdn.net/jjjqqq123321/article/details/136013950

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_67615.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注