skip to content

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 pageshow

explore

questions

page 2 of 2

In CMake, why does an exported library wrap its include directory in $<BUILD_INTERFACE> and $<INSTALL_INTERFACE>, and what happens without them?

level: seniorimportance: should knowfreq 50%

basics

~20 s

The 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.

open as a page

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?

level: seniorimportance: should knowfreq 40%

basics

~20 s

find_* 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.

open as a page

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?

level: seniorimportance: should knowfreq 55%

basics

~20 s

The 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.

open as a page

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?

level: seniorimportance: should knowfreq 40%

basics

~10 s

geomConfig.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.

open as a page

In CMake, what makes an installed geom package relocatable, so it still works after its install prefix is moved or unpacked elsewhere?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Every 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.

open as a page

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?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Raising 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.

open as a page

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?

level: seniorimportance: should knowfreq 47%

basics

~20 s

An 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.

open as a page

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?

level: seniorimportance: should knowfreq 52%

basics

~20 s

A 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.

open as a page

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?

level: seniorimportance: should knowfreq 38%

basics

~20 s

PRIVATE 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.

open as a page

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?

level: seniorimportance: should knowfreq 42%

basics

~10 s

Set 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.

open as a page

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?

level: principalimportance: should knowfreq 40%

basics

~20 s

Write 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.

open as a page

As a C++ team lead, how would you standardize CMake presets and the cmake_minimum_required range across developers and CI?

level: principalimportance: should knowfreq 35%

basics

~20 s

Commit 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.

open as a page

In CMake, how do you enable interprocedural optimization only for a target's Release configuration, and why call check_ipo_supported first?

level: seniorimportance: nice to knowfreq 32%

basics

~20 s

Include 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.

open as a page

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?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

Set 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.

open as a page

showing 31–45 of 45