CMake is an open-source, cross-platform tool designed to manage the build process of software projects. It plays a crucial role in ROS (Robot Operating System) as the primary build system for managing and building ROS packages. ROS uses a build tool called catkin, which is built on top of CMake, to organize, configure, and compile the code in a ROS workspace.
Demo how to use CMake (catkin) in ROS to build a package.
Step 1: create a package named my_robot_pkg that relies on std_msgs and roscpp
catkin_create_pkg my_robot_pkg std_msgs roscpp
Step2 : the CMake file is generated and to be interpreted, a typical CMakelists.txt contains a. project declaration, b. find dependencies, c declare Catkin package, d. include directories, e. add executables and link libraries, f. install targets.
Step 3: once the CMakelist.txt file is created, build the package using the catkin_make command, This invokes CMake to generate the necessary build files for your package, compiles the source code, and produces the executables or libraries.
cd ~/catkin_ws
catkin_make
Step 4: ready to run the node in the package
rosrun my_robot_pkg my_robot_node
Step 5: Modify the CMakeLists.txt file to handle additional dependencies or changes in the code structure as your ROS project grows.
Here’s a simple example CMakeLists.txt for a ROS package named robot_controller:
cmake_minimum_required(VERSION 3.0.2)
project(robot_controller)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(controller_node src/controller_node.cpp)
target_link_libraries(controller_node
${catkin_LIBRARIES}
)
install(TARGETS controller_node
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
Using package building as example here, a typical package in ROS include:
CMakeLists.txt: Defines how the package should be built using CMake. package.xml: Defines metadata and dependencies, like what other ROS packages or libraries are required. src/: Directory where the source code files (C++ or Python) are stored. launch/: Directory containing launch files that define how to run multiple nodes together.