Intermediate level · Module 5 of 6 · 13 guided hours
Builds and Debugging with CMake and GDB
Organise multi-file projects with CMake, debug with a debugger, and catch memory and undefined-behaviour bugs with sanitizers.
Learning objectives
- Structure a multi-file project and build it with CMake
- Debug programs with breakpoints, stepping and watches
- Find memory errors and undefined behaviour with sanitizers
Topics
- translation units and headers
- CMakeLists.txt
- out-of-source builds
- debug vs release builds
- gdb / IDE debugger
- AddressSanitizer and UndefinedBehaviorSanitizer
Lessons
-
Lesson 1 Multi-File Projects and CMake
Organise code into headers and sources, and describe the build with CMake so it works on any machine.
CMakeLists.txtadd_executableout-of-source buildCMAKE_BUILD_TYPE
-
Lesson 2 Debugging with a Debugger
Use breakpoints, stepping, watches and the call stack to find logic errors quickly.
breakpointstep over / into / outwatch expressioncall stack
-
Lesson 3 Sanitizers and Undefined Behaviour
Use AddressSanitizer and UndefinedBehaviorSanitizer to catch memory errors and undefined behaviour that tests may miss.
undefined behaviourAddressSanitizerUndefinedBehaviorSanitizeruse-after-free
Practical activity
Convert the pharmacy program to a CMake project, then find three seeded bugs (an off-by-one, a dangling reference and an uninitialised variable) with the debugger and sanitizers.
Expected outcomes
- Projects others can build with one command
- A systematic approach to finding bugs
Assessment
Practical: build a provided project with CMake and locate two seeded bugs, explaining the tool output that revealed each.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.