Skip to content

/commands/cmake/list-cache-variables

cmake -LAH: list CMake cache variables and options of a project

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
CMake
versions
CMake 3.x, CMake 3.31+ (-LR), CMake 4.x
os
linux, macos, windows

To list the cache variables of a configured CMake project, run cmake -L -N build. -L lists every cache entry that is neither advanced nor internal, -N only reads CMakeCache.txt without configuring again, and adding A and H (-LAH) also shows advanced variables and each variable's help text. These are the values you can change with -D NAME=value.

The variants

CommandShows
cmake -L -N buildnon-advanced cache variables as NAME:TYPE=value
cmake -LH -N buildthe same with the help string of each variable above it
cmake -LA -N buildalso advanced variables (compiler paths, flags, tool locations)
cmake -LAH -N buildeverything above, with help
cmake -LR REGEX -N buildonly variables whose name matches REGEX (CMake 3.31+)
cmake -L -S . -B buildwithout -N, CMake configures first and then lists

List the options of a project

bash
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.20)
project(demo NONE)
option(WITH_DOCS "Build the documentation" ON)
set(LOG_LEVEL "info" CACHE STRING "Default log level")
set_property(CACHE LOG_LEVEL PROPERTY STRINGS debug info warn)
set(TOOL_PATH "" CACHE FILEPATH "Path to an external tool")
mark_as_advanced(TOOL_PATH)
set(NOT_CACHED "x")
EOF
cmake() { uv run -q --no-project --python 3.13 --with cmake==4.4.4 --with ninja==1.13.0 cmake "$@"; }
cmake -S . -B build -G Ninja > /dev/null
echo '--- -L';  cmake -L -N build | grep -v '^CMAKE_INSTALL_PREFIX'
echo '--- -LH'; cmake -LH -N build | grep -A1 'log level'
echo '--- -LA, only TOOL_PATH'; cmake -LA -N build | grep TOOL_PATH
echo '--- -LR'; cmake -LR '^WITH_' -N build

Output

--- -L
-- Cache values
LOG_LEVEL:STRING=info
WITH_DOCS:BOOL=ON
--- -LH
// Default log level
LOG_LEVEL:STRING=info
--- -LA, only TOOL_PATH
TOOL_PATH:FILEPATH=
--- -LR
-- Cache values
WITH_DOCS:BOOL=ON

The first listing leaves out CMAKE_INSTALL_PREFIX, which every project has and whose default differs per OS (see cmake --install --prefix). TOOL_PATH appears only with A because mark_as_advanced() hides it, and NOT_CACHED never appears: a plain set() is not a cache entry. The type after the colon (BOOL, STRING, PATH, FILEPATH) is what option() or set(... CACHE TYPE ...) declared.

Change a value and check it

bash
cmake -S . -B build -DLOG_LEVEL=debug -DWITH_DOCS=OFF
cmake -LR '^(LOG_LEVEL|WITH_DOCS)$' -N build

A value set with -D is written to the cache and kept for every later configure of that build directory, even when you no longer pass it. To return to the project defaults, remove the entry with -U NAME or reconfigure with --fresh, see cmake clean build.

Other ways to read the cache

  • ccmake build (Linux, macOS) and cmake-gui show the same list in an editor; press t in ccmake to toggle advanced entries.
  • grep '^NAME' build/CMakeCache.txt reads the file directly; the cache also holds INTERNAL entries that -L never shows.
  • Inside CMakeLists.txt, message(STATUS "LOG_LEVEL=${LOG_LEVEL}") prints the value during configure.

Pitfalls

  • Without -N, -L reconfigures. On a project that fails to configure you get the error instead of the list; add -N to read the existing cache.
  • Wrong directory. -N on a directory without CMakeCache.txt lists nothing useful; pass the build directory, not the source directory.
  • Build type. A CMAKE_BUILD_TYPE value in the list only matters with single-config generators (Ninja, Makefiles); multi-config generators such as Visual Studio pick the configuration at build time, see CMAKE_BUILD_TYPE.

FAQ

How do I see the options a project offers before building it?

Configure it once into a build directory, then run cmake -LH -N build. Every option() and cache variable the project defines shows up with its help string and current value.

Why is a variable I set with set() not listed?

A plain set(VAR value) creates a normal variable that lives only during configuration. Only set(... CACHE ...) and option() write to CMakeCache.txt, and only cache entries are listed.

Can I edit CMakeCache.txt by hand?

It works, but -D on the command line or ccmake / cmake-gui is safer: they check the type and trigger the dependent re-configuration. After a manual edit, run cmake on the build directory again.

Sources