C++ 23 cheatsheet

C++ 23: Build and debug

10 common C++ 23 tasks.

Compile a program

Build main.cpp into an executable with C++23, warnings and optimization on.

g++ -std=c++23 -Wall -Wextra -Wpedantic -O2 main.cpp -o app

Run the program

Run the executable you just built from the current folder.

./app

Compile several source files

Compile and link more than one source file into one program.

g++ -std=c++23 main.cpp util.cpp -o app

Compile to an object file

Compile one file without linking, which speeds up rebuilds of large projects.

g++ -std=c++23 -c util.cpp -o util.o

Build for debugging

Add debug symbols and disable optimization so a debugger can step through the code.

g++ -std=c++23 -g -O0 main.cpp -o app

Find memory bugs with sanitizers

Detect out-of-bounds access, use-after-free and undefined behavior at runtime.

g++ -std=c++23 -g -fsanitize=address,undefined main.cpp -o app

Debug with gdb

Start the debugger, then use break main, run, next, step, print and backtrace.

gdb ./app

Check for memory leaks

Run a program under Valgrind and report leaks in detail.

valgrind --leak-check=full ./app

Format the code

Reformat source files in place using the .clang-format style.

clang-format -i src/*.cpp src/*.h

Lint the code

Run static analysis and style checks on a file.

clang-tidy main.cpp -- -std=c++23

Was this page helpful?