ROS 2 Lyrical lab 03 ยท 60 min
Build an ament C++ package
Create a package with declared dependencies and an installable rclcpp executable.
Before you start
- A working ~/robot_ws
- C++ Beginner lesson 1
Files you will touch
src/robot_basics/package.xmlsrc/robot_basics/CMakeLists.txtsrc/robot_basics/src/status_node.cpp
Step 1
Set up
cd ~/robot_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 robot_basics --dependencies rclcpp std_msgsStep 2
Create the file
#include <memory>
#include "rclcpp/rclcpp.hpp"
class StatusNode final : public rclcpp::Node {
public:
StatusNode() : Node("status_node") {
RCLCPP_INFO(get_logger(), "robot basics ready");
}
};
int main(int argc, char ** argv) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<StatusNode>());
rclcpp::shutdown();
return 0;
}Step 3
Build, run and inspect
# Add an executable and install rule in CMakeLists.txt, then:
cd ~/robot_ws
colcon build --packages-select robot_basics --symlink-install
source install/setup.bash
ros2 run robot_basics status_nodeProof that it works
- The package builds without warnings
- ros2 run locates status_node
- The log prints robot basics ready
Recovery notes
- If the executable is not found, verify install(TARGETS status_node DESTINATION lib/${PROJECT_NAME}).
- If a header is missing, add both find_package and ament_target_dependencies entries.