Print All Available Variables in CMake

Print All Available Variables in CMake

CMake, a powerful cross-platform build system, allows developers to manage the build process of their projects efficiently. Understanding and inspecting the variables used within a CMake project is crucial for debugging and optimization. This tutorial shows how to print all available variables in CMake.

Using the get_cmake_property function, we can get the list of CMake variables currently defined.

In the following CMake script, we retrieve all CMake variables and stores their names in the variable_names list, which is then sorted alphabetically. Finally, a loop iterates through the sorted list of variable names, and for each variable, a status message is printed, displaying both the variable name and its current value.

cmake_minimum_required(VERSION 3.27)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME} main.cpp)

get_cmake_property(variable_names VARIABLES)
list(SORT variable_names)
foreach (variable_name ${variable_names})
    message(STATUS "${variable_name}=${${variable_name}}")
endforeach ()

This allows developers to inspect and understand the values of all CMake variables during the configuration process.

Output example:

-- CMAKE_ADDR2LINE=/usr/bin/addr2line
-- CMAKE_AR=/usr/bin/ar
-- CMAKE_AR=/usr/bin/ar
-- CMAKE_AUTOGEN_ORIGIN_DEPENDS=ON
-- CMAKE_AUTOMOC_COMPILER_PREDEFINES=ON
-- CMAKE_AUTOMOC_MACRO_NAMES=Q_OBJECT;Q_GADGET;Q_NAMESPACE;Q_NAMESPACE_EXPORT
-- CMAKE_AUTOMOC_PATH_PREFIX=OFF
-- CMAKE_BASE_NAME=g++
...

Leave a Comment

Cancel reply

Your email address will not be published.