CMake
Purpose
Guide agents through modern (target-first) CMake for C/C++ projects: out-of-source builds, dependency management, generator selection, and integration with CI and IDEs.
Triggers
- "How do I write a CMakeLists.txt for my project?"
- "How do I add an external library with CMake?"
- "CMake can't find my package / library"
- "How do I enable sanitizers in CMake?"
- "How do I cross-compile with CMake?"
- "How do I use CMake Presets?"
Workflow
1. Modern CMake principles
- Define targets, not variables. Use
target_*commands. - Use
PUBLIC/PRIVATE/INTERFACEto control property propagation. - Never use
include_directories()orlink_libraries()(legacy). - Minimum CMake version:
cmake_minimum_required(VERSION 3.20)for most features.
2. Minimal project
3. Static / shared libraries
4. Configure and build
Build types: Debug, Release, RelWithDebInfo, MinSizeRel.
5. External dependencies
find_package (system-installed libraries)
FetchContent (download and build dependency)
pkg-config fallback
6. Compiler options by configuration
Generator expressions: $<condition:value> evaluated at build time.
7. Enable sanitizers
Build: cmake -DENABLE_ASAN=ON -S . -B build-asan && cmake --build build-asan
8. Cross-compilation toolchain file
9. CMake Presets (CMake 3.20+)
10. Common errors
For a complete CMakeLists.txt template, see references/templates.md [blocked].
Related skills
- Use
skills/build-systems/ninjafor Ninja generator details - Use
skills/build-systems/makefor Make generator - Use
skills/compilers/cross-gccfor cross-compilation toolchain setup - Use
skills/runtimes/sanitizersfor sanitizer integration details


