16 — Tooling: CMake, Debuggers, Sanitizers, Testing

16 — Tooling: CMake, Debuggers, Sanitizers, Testing

Core Idea

Modern C++ skill includes tooling. Real C++ work is not just language syntax.

Compiler Flags

Start with strict warnings:

-Wall -Wextra -Wpedantic

During learning, consider:

-Werror

Debug build:

-g -O0

Release build:

-O2

CMake

Minimal project:

cmake_minimum_required(VERSION 3.20)
project(myapp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(myapp src/main.cpp)

Build:

cmake -S . -B build
cmake --build build
./build/myapp

Debuggers

Use gdb or lldb.

Basic gdb commands:

break main
run
next
step
print variable
backtrace
continue

Sanitizers

Address + UB sanitizer:

-fsanitize=address,undefined -fno-omit-frame-pointer -g

Thread sanitizer:

-fsanitize=thread -g

Use sanitizers regularly during development.

Formatting

Use clang-format to avoid style arguments.

clang-format -i src/*.cpp include/*.hpp

Static Analysis

Use clang-tidy to catch suspicious patterns.

clang-tidy src/main.cpp -- -std=c++20

Testing

Popular options:

  • Catch2,
  • GoogleTest,
  • doctest.

Tiny standard-library smoke test:

#include <cassert>

void test_addition() {
    assert(add(2, 3) == 5);
}

Package Managers

Common C++ package managers:

  • vcpkg,
  • Conan.

You do not need them on day one, but real projects often use them.

  • C++20 compiler: GCC or Clang.
  • CMake.
  • Ninja or Make.
  • gdb/lldb.
  • clang-format.
  • clang-tidy.
  • sanitizers.
  • Catch2 or GoogleTest.

Quick Self-Test

You should be able to answer:

  • How do you build a CMake project?
  • How do you set a breakpoint?
  • What does AddressSanitizer catch?
  • Why use clang-format?
  • What is the difference between debug and release builds?

00 / The Agent

The chat box that lives on the blog.

running on Cloudflare · free tier

A tiny JS island posting to a Cloudflare Worker that streams answers from a free Nemotron endpoint. No origin server. No database. The static site stays static — this one box is the only thing that breathes.