cmake
Master modern CMake for C/C++ with target-first principles, out-of-source builds, and dependency management via find_package or FetchContent. Configure generators like Ninja and Make, set up sanitizers, cross-compile with toolchain files, and leverage CMake Presets for reproducible builds.
CMake guides you through modern target-first build configuration for C/C++ projects, from dependency management to cross-compilation.
AI-generated summary based on this skill's SKILL.md
Install
mohitmishra786/low-level-dev-skills/cmake · repository language: JavaScript
git clone https://github.com/mohitmishra786/low-level-dev-skills
cp -r low-level-dev-skills/skills/build-systems/cmake ~/.claude/skills/cmakeFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How do I write CMakeLists.txt for a C/C++ project?
cmake uses a target-first approach. Start with cmake_minimum_required() and project(), then define targets using add_executable() or add_library(). Use target_include_directories(), target_link_libraries(), and target_compile_options() to configure each target's properties. Apply PUBLIC, PRIVATE, or INTERFACE keywords to control visibility. cmake supports out-of-source builds—create a separate build directory, run cmake from there, and keep source clean. This modern pattern scales better than older file-level commands.
What's the difference between find_package and fetchcontent in cmake?
cmake find_package locates pre-installed packages on your system via Find modules or config files, ideal for system libraries. FetchContent downloads and builds dependencies at configure time, giving you version control and reproducibility. Use find_package for optional or system-provided libraries; use FetchContent for vendored or tightly-versioned dependencies. cmake Presets can pin both strategies for team consistency.
How do I configure cmake to handle external dependencies and packages?
cmake Configure external dependencies via find_package(PackageName) or FetchContent_Declare(). For find_package, ensure the package's config or Find module is in CMAKE_PREFIX_PATH. Handle missing packages gracefully with REQUIRED or QUIET flags. Use target_link_libraries() to attach dependencies to your targets. For complex setups, cmake Presets define toolchain paths and cache variables, and cpack handles installation rules and distribution.
How do I set up cross-compilation with cmake toolchain files?
cmake cross-compilation requires a toolchain file specifying the target architecture. Create a file (e.g., aarch64-toolchain.cmake) setting CMAKE_SYSTEM_NAME, CMAKE_SYSTEM_PROCESSOR, CMAKE_C_COMPILER, and CMAKE_CXX_COMPILER. Pass it via cmake -DCMAKE_TOOLCHAIN_FILE=aarch64-toolchain.cmake. Define CMAKE_FIND_ROOT_PATH to guide dependency search. cmake Presets can embed toolchain references for reproducible arm or other cross-builds.
How do I enable address sanitizer and configure debug/release builds?
cmake Enable AddressSanitizer by adding compiler flags: target_compile_options(mytarget PRIVATE -fsanitize=address -g). For debug/release, set CMAKE_BUILD_TYPE (single-config) or use multi-config generators like Visual Studio. Use generator expressions like $<CONFIG:Debug> to apply flags conditionally. cmake Presets simplify this: define separate preset entries for debug (with sanitizer) and release builds, ensuring consistent configuration across your team.
What should I do if cmake can't find a package I need?
cmake Configure error "can't find package" usually means the package's config or Find module isn't in CMAKE_PREFIX_PATH. Check the package's installation location and add its directory to CMAKE_PREFIX_PATH via -DCMAKE_PREFIX_PATH=/path/to/package. Verify the Find module name matches find_package() exactly. Use cmake --debug-output to trace search paths. Alternatively, use FetchContent to download the dependency directly, bypassing system search entirely.
SKILL.md
rendered from the published skill — quoted content, verbatim
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
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 2 files
skills/build-systems/cmake/SKILL.md
skills/build-systems/cmake/references/templates.md