GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com # C and C++
> **Read this when** you write C++ for a ROS 2 package or a robot SDK (Unitree Go2, Dynamixel), or C for firmware, and want your editor, your reviewer and CI to agree on style.
We follow the ROS 2 code style everywhere, including code that never touches ROS, so one `.clang-format` and one `.clang-tidy` serve every lab repo. The one thing to remember: format with clang-format, and in ROS 2 packages run `ament_clang_format` instead of `ament_uncrustify`, because the two formatters cannot both be satisfied.
## Contents
- [The style we follow](#the-style-we-follow)
- [Language versions](#language-versions)
- [Naming and file layout](#naming-and-file-layout)
- [Install the tools](#install-the-tools)
- [Editor setup](#editor-setup)
- [Adopt the config in a repo](#adopt-the-config-in-a-repo)
- [Check in CI](#check-in-ci)
- [Common mistakes](#common-mistakes)
- [Related](#related)
## The style we follow
The [ROS 2 code style](https://docs.ros.org/en/jazzy/The-ROS2-Project/Contributing/Code-Style-Language-Versions.html) builds on the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) with a few changes. We use it because most of our C++ lives in ROS 2 packages, and matching the ecosystem means code moves between lab repos and upstream projects without a reformat.
The changes from Google style that you will notice:
| Rule | ROS 2 style |
|---|---|
| Line length | 100 characters |
| Braces | Open brace on its own line for function, class, struct and enum definitions; on the same line for `if`, `else`, `for`, `while` |
| Always braces | Even for one-line `if` and loop bodies |
| Pointers and references | `char * c;` and `const Limits & limits` (space on both sides) |
| Access specifiers | `public:` in the same column as `class` |
| Function names | `snake_case` is allowed and is what ROS 2 core packages use; we use it |
| Exceptions | Allowed; avoid them in APIs you plan to wrap in C |
| Boost | Avoid unless there is no alternative |
For C, ROS 2 follows [PEP 7](https://peps.python.org/pep-0007/) (Python's C style) with `//` comments allowed. Our `.clang-format` formats C files with the same settings.
`configs/c-cpp/.clang-format` is the configuration that ROS 2 ships in [ament_clang_format](https://github.com/ament/ament_lint/blob/jazzy/ament_clang_format/ament_clang_format/configuration/.clang-format), unchanged. We checked that clang-format 14 (Ubuntu 22.04), 18 (Ubuntu 24.04), 21 (Ubuntu 26.04) and 23 (the version Homebrew installs) all format our examples the same way; CI pins version 18.
`configs/c-cpp/.clang-tidy` turns on four check families and then removes the checks that are mostly noise in robot code:
| Family | What it catches | Turned off, and why |
|---|---|---|
| `bugprone-*` | Likely bugs: dangling handles, bad `memset`, suspicious string compares | `easily-swappable-parameters` (fires on every function with two `double`s), `narrowing-conversions` (every float/int mix in sensor code), `reserved-identifier` (ROS 2 header guards contain `__`) |
| `clang-analyzer-*` | Null dereferences, memory leaks, uses of uninitialized values (it follows each path through the code) | None |
| `performance-*` | Needless copies, `std::move` misuse | `avoid-endl` |
| `modernize-*` | `nullptr`, `using`, range `for`, `override` | `avoid-bind` (the ROS 2 style allows `std::bind`), `avoid-c-arrays` (vendor SDKs), `use-nodiscard`, `use-trailing-return-type` |
| `readability-*` (16 picked checks) | Missing braces, `size() == 0`, naming, redundant code | Everything else in the family |
`readability-identifier-naming` enforces the naming table below. Every enabled check is an error (`WarningsAsErrors: '*'`), so keep the list short enough that this stays true.
## Language versions
The C and C++ standards depend on the ROS 2 distribution. Set them in `CMakeLists.txt` so every compiler agrees.
| ROS 2 distribution | C++ | C | Source |
|---|---|---|---|
| Humble, Jazzy, Kilted | C++17 | C99 | [Jazzy code style page](https://docs.ros.org/en/jazzy/The-ROS2-Project/Contributing/Code-Style-Language-Versions.html) |
| Lyrical, Rolling | C++20 | C17 | [Rolling code style page](https://docs.ros.org/en/rolling/The-ROS2-Project/Contributing/Contributing-to-code/Code-Style-Language-Versions.html) |
Code that must build on more than one distribution targets the oldest one, so C++17 for anything that still runs on Humble or Jazzy. Firmware uses the standard its board's toolchain supports; check the platform's docs.
## Naming and file layout
| Thing | Style | Example |
|---|---|---|
| Namespaces, packages | `snake_case` | `teleop_velocity_limiter` |
| Classes, structs, enums, type aliases | `CamelCase` | `VelocityLimiter` |
| Functions and methods | `snake_case` | `limit()`, `on_command()` |
| Variables and parameters | `snake_case` | `dt_seconds` |
| Private and protected members | `snake_case_` with a trailing underscore | `last_command_` |
| Global variables | `g_` prefix | `g_shutdown_requested` |
| Macros | `UPPER_CASE` | `RING_BUFFER_CAPACITY` |
| Constants and enum values | Any consistent style in the package (ROS 2 mixes `kName`, `NAME` and `name`) | `kMaxSpeed` |
| Header guards | `PACKAGE__PATH__FILE_HPP_` (what `ament_cpplint` expects) | `TELEOP_VELOCITY_LIMITER__VELOCITY_LIMITER_HPP_` |
| Files | `snake_case.hpp` and `snake_case.cpp`; C uses `.h` and `.c` | `velocity_limiter_node.cpp` |
A ROS 2 C++ package keeps public headers in `include/ |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |