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 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(TARGETS ${PROJECT_NAME}_mapOptimizationDESTINATION lib/${PROJECT_NAME}
)
是将可执行程序 ${PROJECT_NAME}_mapOptimization安装在./install/${PROJECT_NAME}/lib/${PROJECT_NAME}/目录下。
放在别的路径下,启动ros2节点的时候会提示找不到。
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保持一致。