How to keep C++ and CMakeList codes to be clean

April 23, 2023
When collaborating with others on a software project, it's important to ensure that the codebase is consistent and well-formatted. This can be achieved through the use of code formatting tools such as cmake-format and clang-format. cmake-format is a tool that helps to format CMake files in a consistent and readable manner. clang-format, on the other hand, is a tool that can be used to format C++, Java, JavaScript, and other code in a consistent style. By using these tools, developers can ensure that the codebase is consistent and easy to read, which can make it easier for other developers to understand and work with the code. This can be especially important when working on large projects with many contributors, where inconsistent formatting can lead to confusion and errors.

When collaborating with others on a software project, it's important to ensure that the codebase is consistent and well-formatted. This can be achieved through the use of code formatting tools such as cmake-format and clang-format. cmake-format is a tool that helps to format CMake files in a consistent and readable manner. clang-format, on the other hand, is a tool that can be used to format C++, Java, JavaScript, and other code in a consistent style.

By using these tools, developers can ensure that the codebase is consistent and easy to read, which can make it easier for other developers to understand and work with the code. This can be especially important when working on large projects with many contributors, where inconsistent formatting can lead to confusion and errors.

Installation of clang-format and cmake-format

clang-format

sudo apt-get install clang-format

Then check installation:

/bin$ ls | grep clang

cmake-format

pip3 install cmake-format
  • Note: you can either pip3 install cmake_format using underbar.

  • After installation, we should add PATH variable if the prompt says:

    Installing collected packages: cmakelang
    WARNING: The scripts cmake-annotate, cmake-format, cmake-genparsers, cmake-lint and ctest-to are installed in '/home/jbs/.local/bin' which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

    To handle this, add export PATH="~/.local/bin:$PATH" to ~/.bashrc.

VSCode setup

We have to install two extensions: clang-foramt (opens in a new tab) and cmake-format (opens in a new tab).

Applying formatters using vscode

.
β”œβ”€β”€ .clang-format
β”œβ”€β”€ .cmake-format.yaml
β”œβ”€β”€ my_package_core
└── my_package_ros2

1. clang-format

clang-format is just works out-of-box when .clang-format is located in your project root folder (refer this (opens in a new tab)).

2. cmake-format

In case of cmake-format, we have to modify the executable path (Exe Path) of cmake-format:

When you setup and make .cmake-format.yaml in your project folder (example is here (opens in a new tab)), extension cmake-format will work when saving your CMakeLists.txt file.