ros2迁移c++之package.xml、CMakeLists.txt及编译

1、package.xml

<package>               <!--1. 根标签-->
	<name>              <!--2. 包名-->
	<version>           <!--3. 版本号-->
	<description>       <!--4. 包描述-->
	<maintainer>        <!--5. 维护者-->
	<license>           <!--6. 软件许可-->
	<buildtool_depend>  <!--7. 编译构建工具,通常为catkin-->
	<depend>            <!--8. 指定依赖项为编译、 导出、 运行需要的依赖,最常用的依赖标记。-->
	<build_depend>      <!--9. 编译依赖项-->
	<build_export_depend> <!--10. 导出依赖项-->
	<exec_depend>         <!--11. 运行依赖项-->
	<test_depend>         <!--12. 测试用例依赖项-->
	<doc_depend>          <!--13. 文档依赖项-->
</package>                <!--根标签-->

ros2和ros1的基本通用,在ros2中由于编译模式的更改,需要删除catkin的相关内容,更改为ament_cmake

  <!-- ROS1 -->
  <buildtool_depend>catkin</buildtool_depend>
  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->
  </export>
----------------------------------------------------------------------
  <!-- ROS2 -->
  <buildtool_depend>ament_cmake</buildtool_depend>
  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>
  <export>
    <build_type>ament_cmake</build_type>
  </export>

2、CMakeLists.txt

2.1 find_package

# ros1
find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES test_library
  CATKIN_DEPENDS roscpp std_msgs
#  DEPENDS system_lib
)
----------------------------------------------------------------------
# ros2
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
set(dependencies
  rclcpp
  std_msgs
)
# ament_target_dependencies是官方推荐的方式去添加依赖项。它将使依赖项的库、头文件和自身的依赖项被正常找到
ament_target_dependencies(
  ${dependencies}
)

2.2 include_directories

# ros1
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)
----------------------------------------------------------------------
# ros2
include_directories(
  include
)

2.3 动态库

# ros1
add_library(test_library
  src/test_library.cpp
)
add_dependencies(test_library ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(test_library
  ${catkin_LIBRARIES}
)
----------------------------------------------------------------------
# ros2
add_library(test_library SHARED
  src/test_library.cpp
)
# 通常来说,若依赖项为ROS2功能包时,则使用ament_target_dependencies。若功能包有多个库,它也将一并包含。
ament_target_dependencies(test_library 
  ${dependencies} # dependencies与前面提到的一致
)
# 链接自定义依赖库
target_link_libraries(test_library 
  lib1
)

2.4 可执行程序

# ros1
add_executable(test_node 
  src/main.cpp
)
add_dependencies(test_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(test_node 
    test_library 
    ${catkin_LIBRARIES}
)
----------------------------------------------------------------------
# ros2
add_executable(test_node 
	src/main.cpp
)
ament_target_dependencies(test_node 
  ${dependencies}
)
target_link_libraries(test_node 
	test_library 
)

2.5 install库及可执行文件

# ros1
# 安装库
install(TARGETS
  lib1
  lib2
  lib3
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
# 安装可执行文件
install(TARGETS
  node1
  node2
  node3
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
# 安装头文件
install(DIRECTORY 
  include/${PROJECT_NAME}/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  FILES_MATCHING PATTERN "*.h"
  PATTERN ".svn" EXCLUDE
)
# 安装launch及其他文件
install(DIRECTORY launch
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
----------------------------------------------------------------------
# ros2---set方法在ros1中也可使用
# 安装库
set(librarys
  lib1 
  lib2 
  lib3 
)
install(TARGETS
  ${librarys}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)
# 安装可执行文件
set(nodes
  node1 
  node2 
  node3 
)
install(TARGETS
  ${nodes}
  RUNTIME DESTINATION lib/${PROJECT_NAME}
)
# 安装头文件
install(DIRECTORY include/
  DESTINATION include/
)
# 安装launch及其他文件
install(DIRECTORY launch file
  DESTINATION share/${PROJECT_NAME}
)

2.6 标记头文件

# ros1
----------------------------------------------------------------------
# ros2
ament_export_include_directories(include)

2.7 供其他节点链接引用

# ros1
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES lib1 lib2 lib3
  CATKIN_DEPENDS roscpp rospy std_msgs 
#  DEPENDS system_lib
)
----------------------------------------------------------------------
# ros2
set(librarys
  lib1 
  lib2 
  lib3 
)
# 其他的功能包能链接到这些库
ament_export_libraries(${librarys})
# ament_export_dependencies会将依赖项导出到下游软件包
ament_export_dependencies(${dependencies})

2.8 项目安装

# ros1
----------------------------------------------------------------------
# ros2--ament_package是CMakeLists.txt文件中的最后一个调用
ament_package()

3、编译

# ros1
# 编译整个工作空间
catkin_make -DCATKIN_WHITELIST_PACKAGES = ""
# 编译单独节点
catkin_make -DCATKIN_WHITELIST_PACKAGES = "pkg1"
----------------------------------------------------------------------
# ros2--colcon其余命令可自行查阅
# 编译整个工作空间
colcon build --symlink-install
# 编译单独节点
colcon build --packages-select pkg1
posted @ 2023-09-14 17:26  泡泡吐泡泡啊  阅读(246)  评论(0编辑  收藏  举报