Toolkit: Debugging a Configure
Most CMake bugs are configure-time bugs: a package found in the wrong place, a variable that is empty when you expected a value, an option silently ignored. This lesson collects the switches and helpers that make them visible, from quick prints to the find-debugging mode.
Print helpers: CMakePrintHelpers
message("x = ${x}") gets tedious. cmake_print_variables prints several variables with their names, and cmake_print_properties prints target (or source, test, directory…) properties:
mkdir -p dbg && cd dbg
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Dbg VERSION 1.0 LANGUAGES CXX)
include(CMakePrintHelpers)
add_library(core STATIC core.cpp)
target_compile_definitions(core PUBLIC CORE_API=1)
cmake_print_variables(PROJECT_VERSION CMAKE_BUILD_TYPE CMAKE_CXX_COMPILER_ID)
cmake_print_properties(TARGETS core PROPERTIES TYPE INTERFACE_COMPILE_DEFINITIONS)
CM
touch core.cpp
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug 2>&1 | grep -E 'PROJECT_VERSION|Properties|core\.'-- PROJECT_VERSION="1.0" ; CMAKE_BUILD_TYPE="Debug" ; CMAKE_CXX_COMPILER_ID="GNU"
Properties for TARGET core:
core.TYPE = "STATIC_LIBRARY"
core.INTERFACE_COMPILE_DEFINITIONS = "CORE_API=1"
-Wuninitialized: catch typos in variable names
A misspelled variable expands to an empty string without complaint. -Wuninitialized reports every use of a variable that was never set. Older documentation calls it --warn-uninitialized; CMake 4.4 still accepts that spelling but says it is deprecated:
mkdir -p typo && cd typo
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Typo LANGUAGES NONE)
set(INSTALL_DOCS ON)
if(INSTAL_DOCS) # typo: never true, never an error
message("installing docs")
endif()
message("prefix: ${CMAKE_INSTAL_PREFIX}")
CM
cmake -S . -B build --warn-uninitialized --log-level=NOTICE 2>&1 | grep -v '^--' | head -n 1
cmake -S . -B build2 -Wuninitialized --log-level=NOTICE 2>&1 | grep -v '^--'The --warn-uninitialized option is deprecated. Use -Wuninitialized instead.
CMake Warning (uninitialized) at CMakeLists.txt:7 (message):
uninitialized variable 'CMAKE_INSTAL_PREFIX'
prefix:
The ${CMAKE_INSTAL_PREFIX} typo was reported. The one in if(INSTAL_DOCS) was not: if() evaluates a bare name without expanding it, so it's not a "use" of an uninitialized variable. Writing if(DEFINED …) guards, or using option() (whose name is validated by the user setting it), and reviewing with --trace-expand catch those. -Wuninitialized is worth adding to a CI "lint" configure of your own project (it also reports variables used inside CMake's modules, so filter by file).
--debug-find: why wasn't my package found?
--debug-find prints every path that find_package, find_library, find_path and find_program considered. It's very long; --debug-find-pkg=<Name> and --debug-find-var=<VAR> restrict it:
mkdir -p sdk/share/greet && mkdir -p app && cd app
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(App LANGUAGES NONE)
find_package(Greet CONFIG)
message("Greet_FOUND=${Greet_FOUND}")
CM
cmake -S . -B build --debug-find-pkg=Greet -DCMAKE_PREFIX_PATH=$HOME/sdk 2>&1 \
| grep -E 'CMAKE_PREFIX_PATH variable|/home/learner/sdk|Greet_FOUND' | sed -n 1,8p CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].
/home/learner/sdk
/home/learner/sdk/GreetConfig.cmake
/home/learner/sdk/greet-config.cmake
/home/learner/sdk/lib/cps/Greet.cps
/home/learner/sdk/lib/cps/greet.cps
/home/learner/sdk/share/cps/Greet.cps
/home/learner/sdk/share/cps/greet.cps
The output lists, per search step (<Name>_ROOT, CMAKE_PREFIX_PATH, environment variables, system paths…), the prefixes tried and the exact candidate directories. Here it shows that ~/sdk was searched, but no GreetConfig.cmake was there, so the fix is in the package, not in the consumer.
Status checks: CHECK_START / CHECK_PASS / CHECK_FAIL
For your own long-running configure logic, message(CHECK_*) prints the familiar "Looking for X - found" lines, properly indented when nested:
mkdir -p chk && cd chk
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Chk LANGUAGES NONE)
message(CHECK_START "Detecting GPU backends")
list(APPEND CMAKE_MESSAGE_INDENT " ")
foreach(api Vulkan Metal OpenGL)
message(CHECK_START "Looking for ${api}")
if(api STREQUAL "OpenGL")
message(CHECK_PASS "found")
else()
message(CHECK_FAIL "not found")
endif()
endforeach()
list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "using OpenGL")
CM
cmake -S . -B build | grep -v -e '^-- Configuring' -e '^-- Generating' -e '^-- Build files'-- Detecting GPU backends
-- Looking for Vulkan
-- Looking for Vulkan - not found
-- Looking for Metal
-- Looking for Metal - not found
-- Looking for OpenGL
-- Looking for OpenGL - found
-- Detecting GPU backends - using OpenGL
Starting clean
A surprising number of "CMake bugs" are stale cache entries: a -D from last week, a find_library result from before you installed the right version, check results from another compiler. Before debugging anything deeper:
cmake --fresh -S . -B build # re-configure with an empty cache (3.24+)
rm -rf build # or simply start overThe debugger
CMake 3.27+ implements the Debug Adapter Protocol: cmake --debugger --debugger-pipe <pipe> lets an IDE set breakpoints in CMakeLists.txt, step through commands and inspect variables. VS Code's CMake Tools ("CMake: Configure with CMake Debugger") and Visual Studio 2022 use it; it's the most comfortable way to debug a complicated configure, see the next lesson.
Test yourself
Which option reports uses of variables that were never set?
--debug-output-Wuninitialized--trace-Wno-dev
find_package(Foo) fails and you need to see which directories were searched. Which option?
--debug-find-pkg=Foo--graphviz--system-information-LAH
Which module provides cmake_print_variables?
- CMakeDebug
- CMakePrintHelpers
- FeatureSummary
- CheckVariable