CMake
CMake generates native builds for C and C++ from targets with usage requirements, and is the default answer for cross-platform builds. Interviewers check modern, target-based habits.
on this pageshowhide
explore
- Targets and Properties4 questions
- Variables and Cache5 questions
- Package Discovery5 questions
- Generators and Toolchains4 questions
- Install and Export4 questions
- Presets and Policies6 questions
- Generate-Time Expressions4 questions
- Debug and Release Configs4 questions
- CTest and Test Registration4 questions
- Commands, Functions and Macros5 questions
questions
page 2 of 2In CMake, why does an exported library wrap its include directory in $<BUILD_INTERFACE> and $<INSTALL_INTERFACE>, and what happens without them?
basics
~20 sThe headers live in the source tree while building and under the install prefix afterwards. $<BUILD_INTERFACE:...> applies only inside the build, $<INSTALL_INTERFACE:...> only in the installed export; a plain source path makes install(EXPORT) fail at generate time.
In a CMake cross build for an ARM board, how do CMAKE_SYSROOT and the CMAKE_FIND_ROOT_PATH_MODE_* variables keep find_library and find_program from picking the wrong machine's files?
basics
~20 sfind_* commands search the re-rooted prefixes (CMAKE_FIND_ROOT_PATH, CMAKE_SYSROOT) and, by default, the host too. Setting the library, include and package modes to ONLY keeps them inside the target roots, and the program mode to NEVER finds build tools on the host.
When CMake cross-compiles a project from an x86 workstation for a Linux ARM board, what belongs in the toolchain file, and when does CMake read it?
basics
~20 sThe toolchain file describes the target: CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR, the cross compilers, an optional CMAKE_SYSROOT and the find-root modes. It is passed with --toolchain or CMAKE_TOOLCHAIN_FILE when the build tree is first configured, and read before compiler detection.
In CMake, a consumer's find_package(geom) fails with "The link interface of target geom::geom contains: Threads::Threads but the target was not found"; what is missing?
basics
~10 sgeomConfig.cmake never re-finds the packages geom's exported link interface depends on. install(EXPORT) records the name Threads::Threads but does not define it, so the config file must call find_dependency(Threads) before including geomTargets.cmake.
In CMake, what makes an installed geom package relocatable, so it still works after its install prefix is moved or unpacked elsewhere?
basics
~20 sEvery path the package publishes must be computed from the package files' own location, never from the build machine's prefix. install(EXPORT) does this for targets; configure_package_config_file with @PACKAGE_INIT@ and PATH_VARS does it for the config file.
After a CMake project raises its policy version, add_executable(app main) fails with Cannot find source file; why, and how do you migrate policies safely?
basics
~20 sRaising the policy version set CMP0115 to NEW, so CMake stopped guessing the .cpp extension. Fix the code to name main.cpp; migrate by reading each 'Policy CMPxxxx is not set' warning first and only using cmake_policy(SET ... OLD) temporarily.
In CMake, why does an add_custom_command(OUTPUT build_info.h ...) rule never run, and when do you switch to add_custom_target instead?
basics
~20 sAn OUTPUT rule is attached only to a target in the same directory that lists the output as a source, so with no consumer it never runs. Use add_custom_target when the step must run on every build, such as stamping a commit hash.
In CMake, a team's add_library wrapper is written as a macro; what breaks compared with a function, and when is a macro still the right choice?
basics
~20 sA macro's body is substituted into the caller, so its temporaries leak, ARGN and ARGV are text rather than variables, and return() exits the caller. A function gets its own scope. Use a macro only when setting variables in the caller is the point.
In CMake, core is built with target_compile_definitions(core PRIVATE CORE_PROFILING) and core.hpp adds a struct member under #ifdef CORE_PROFILING; why does the app misbehave?
basics
~20 sPRIVATE gives CORE_PROFILING to core's own sources only, so the app compiles core.hpp without it and sees a smaller struct. The two sides disagree on the layout, which is an ODR violation with undefined behaviour; the definition must be PUBLIC.
In CMake, a game adds a vendored physics library with add_subdirectory; how do you turn off its option(PHYSICS_BUILD_TESTS ... ON) without editing its files?
basics
~10 sSet a normal variable, set(PHYSICS_BUILD_TESTS OFF), before add_subdirectory. With policy CMP0077 NEW, option() then does nothing. If the library's minimum version predates 3.13, also set CMAKE_POLICY_DEFAULT_CMP0077 to NEW.
For a C++ team's CMake build, how do you choose between FetchContent and find_package fed by a package manager for third-party dependencies?
basics
~20 sWrite find_package calls as the project's single interface, and let the environment decide how they are satisfied: a package manager or prebuilt prefix for heavy or shared dependencies, FetchContent with FIND_PACKAGE_ARGS as a fallback for small, header-only or tightly pinned ones.
As a C++ team lead, how would you standardize CMake presets and the cmake_minimum_required range across developers and CI?
basics
~20 sCommit a small CMakePresets.json whose hidden bases define the standard generator, build directories and options, make CI run the same presets, and pin cmake_minimum_required to the oldest supported CMake with policy_max at the newest tested; personal variants go in CMakeUserPresets.json.
In CMake, how do you enable interprocedural optimization only for a target's Release configuration, and why call check_ipo_supported first?
basics
~20 sInclude CheckIPOSupported, call check_ipo_supported(RESULT ok), and if ok set the INTERPROCEDURAL_OPTIMIZATION_RELEASE property on the target. Since policy CMP0069, enabling IPO on a compiler CMake has no IPO flags for is an error, so the check keeps unsupported toolchains building.
A test in a CMake suite sometimes hangs in CI and fails about once in fifty runs; how do ctest timeouts and the --repeat modes help you contain and find it?
basics
~10 sSet a TIMEOUT property, or ctest --timeout as a default, so a hang becomes a reported timeout. Then ctest --repeat until-fail:200 on that test reproduces the rare failure; until-pass and after-timeout only tolerate flakiness.
In CMake, core is a STATIC library that links the compress library PRIVATE, so why does libcompress.a still appear on the app's link line?
basics
~20 sA static library is an archive, not a linked binary, so its references into compress are resolved only when app links. CMake therefore records the private dependency as $<LINK_ONLY:compress>: on the link line, but without compress's compile requirements.
showing 31–45 of 45