CMakeLists zum Kompilieren einer ROS2 C++-Anwendung ohne Verwendung des ROS2-Build-Systems
Die folgende CMakeLists.txt funktioniert zum Kompilieren einer C++-Anwendung, die einige der ROS2-Bibliotheken verwendet, ohne das ROS2-Build-System zu verwenden. Dies kann verwendet werden, um ROS2s CMake-Auto-Konfiguration zu umgehen oder spezifische benutzerdefinierte Optionen zu erzwingen.
Sie müssen möglicherweise die Bibliotheks- und Include-Verzeichnisliste anpassen, wenn für Ihre spezifische Anwendung einige Bibliotheken fehlen.
CMakeLists-example.txt
cmake_minimum_required(VERSION 3.8)
# Define the project name and specify C++ standard
project(rclcpp_example_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Specify the source files
add_executable(my_ros2_test main.cpp)
target_include_directories(my_ros2_test PUBLIC
/opt/ros/jazzy/include/rclcpp/
/opt/ros/jazzy/include/rcl_interfaces/
/opt/ros/jazzy/include/rosidl_runtime_c/
/opt/ros/jazzy/include/rosidl_runtime_cpp/
/opt/ros/jazzy/include/builtin_interfaces/
/opt/ros/jazzy/include/rosidl_typesupport_interface/
/opt/ros/jazzy/include/rosidl_dynamic_typesupport/
/opt/ros/jazzy/include/rosidl_typesupport_introspection_cpp/
/opt/ros/jazzy/include/rcutils/
/opt/ros/jazzy/include/rcl/
/opt/ros/jazzy/include/rmw/
/opt/ros/jazzy/include/rcpputils/
/opt/ros/jazzy/include/rcl_yaml_param_parser/
/opt/ros/jazzy/include/type_description_interfaces/
/opt/ros/jazzy/include/tracetools/
/opt/ros/jazzy/include/libstatistics_collector/
/opt/ros/jazzy/include/statistics_msgs/
/opt/ros/jazzy/include/rosbag2_cpp/
/opt/ros/jazzy/include/rosbag2_storage/
/opt/ros/jazzy/include/service_msgs/
/opt/ros/jazzy/include/sensor_msgs/
/opt/ros/jazzy/include/std_msgs/
)
target_link_directories(my_ros2_test PUBLIC /opt/ros/jazzy/lib/)
target_link_libraries(my_ros2_test
rclcpp
rcl
tracetools
rmw_implementation
rcutils
rosbag2_cpp
rosbag2_storage
sensor_msgs__rosidl_typesupport_cpp
)
# Install the target
install(TARGETS my_ros2_test
DESTINATION lib/${PROJECT_NAME}
)If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow