ROS2 builds are slower than ROS1 builds - ROS1 treated the entire workspace as a single CMake project, but ROS2 re-runs CMake for every package using a Python runner, causing lots of work to be re-done.
Colcon accepts --cmake-args
- anything after this is passed to CMake. The most common use of this is to build in Release (or RelWithDebInfo) mode, since CMake builds in Debug mode by default: colcon build --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo
Ninja runs much faster[citation needed]
than Make, which is the default for CMake: -GNinja
By default, compile errors won’t appear in colcon’s output (https://github.com/colcon/colcon-cmake/issues/67) - configure colcon to print all output: --event-handlers console_direct+
(this is not a CMake argument, and should be placed before the --cmake-args
flag)
Clang builds a little faster: -DCMAKE_C_COMPILER=clang-18 -DCMAKE_CXX_COMPILER=clang++-18
If you’re using a newer colcon than I am, you can do this with a mixin: --mixins clang
-DCMAKE_EXPORT_COMPILE_COMMANDS=True
in c_cpp_properties.json
:
{
"configurations": [
{
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
Using the command line arguments is repetitive, and easy to forget. A colcon_defaults.yaml
file can be placed in the project root (https://colcon.readthedocs.io/en/released/user/configuration.html):
{
"build":{
"symlink-install": true,
"cmake-args": [
"-DCMAKE_BUILD_TYPE=RelWithDebInfo",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=True",
"-GNinja",
# clang
"-DCMAKE_C_COMPILER=clang-18",
"-DCMAKE_CXX_COMPILER=clang++-18",
],
"event-handlers": ["console_cohesion+"],
}
Now, we can just run colcon build