This repository provides a solution for the common challenges encountered when developing ROS2 packages: managing CMakeLists.txt and setup.py. If you’ve ever worked with building both Python and C++ executables, you know how tricky it can be to configure these files correctly.
We wanted to eliminate the repetitive work of manually setting up both CMakeLists.txt for C++ and setup.py for Python. The goal? Create a single CMakeLists.txt file that handles everything. Whether you’re working with Python nodes, C++ nodes, or additional resources like URDFs and launch files, this CMakeLists.txt can manage it all.
This approach allows you to focus on building your ROS2 applications without worrying about the build setup.
- Automatically detects and installs Python executables from both the
scripts/directory and Python package-style organization. - Automatically builds and installs C++ executables from the
src/directory. - Handles all additional resources like launch files, URDFs, meshes, and config files.
- Reusable across multiple ROS2 packages with minimal changes.
- Example package included to demonstrate how to use the
CMakeLists.txt.
Clone this repository and use the CMakeLists.txt for your ROS2 package:
git clone https://github.com/SAKErobotics/ros2_reusable_cmakelists.gitPlace this CMakeLists.txt in the root of your ROS2 package. Ensure your package follows this structure:
your_package/
├── CMakeLists.txt # The reusable CMakeLists.txt from this repository
├── package.xml # Your package metadata
├── scripts/ # Python executables (traditional organization)
├── your_package/ # Python package (alternative organization)
├── src/ # C++ source files (nodes)
├── launch/ # Optional launch files
├── urdf/ # Optional URDF files
└── config/ # Optional config files
Modify the package.xml in your ROS2 package to include the correct package metadata and dependencies for your nodes.
Build your package using colcon:
colcon buildSource the workspace:
source install/setup.bashRun your nodes:
- For Python nodes:
ros2 run your_package your_python_node - For C++ nodes:
ros2 run your_package your_cpp_node
An example ROS2 package is provided in the example_package/ directory that demonstrates how to use the CMakeLists.txt. You can explore this example for guidance.
This project is licensed under the MIT License. See the LICENSE file for more details.
Feel free to submit issues or pull requests if you find any bugs or have suggestions for improvements!