# CMake Variables **`set(VAR value)` sets a variable in CMake. Access it with `${VAR}`**. Variables have scope: local (in current file or directory), parent scope (propagate up to including CMakeLists.txt), or cache (persistent across runs, visible in the CMake GUI). Setting and using variables: ```cmake set(SOURCES main.cpp utils.cpp) add_executable(app ${SOURCES}) set(VERSION 1.2.3) message(STATUS "Building version ${VERSION}") ``` Variable scope: ```cmake set(LOCAL_VAR "value") # local: only in current CMakeLists.txt set(PARENT_VAR "value" PARENT_SCOPE) # parent: visible to including file set(CACHE_VAR "value" CACHE STRING "Description") # cache: persistent ``` Local variables don't escape their file. If a subdirectory sets a local variable, it doesn't affect the parent. Use `PARENT_SCOPE` to propagate values upward. Cache variables persist across `cmake` runs and appear in the CMake GUI or via `cmake -DVAR=value`: ```cmake set(MY_OPTION OFF CACHE BOOL "Enable feature X") if(MY_OPTION) # ... enable feature endif() ``` Built-in variables (read-only, set by CMake): ```cmake CMAKE_BUILD_TYPE # "Debug", "Release", "RelWithDebInfo" CMAKE_CXX_COMPILER # path to C++ compiler CMAKE_INSTALL_PREFIX # root directory for installation CMAKE_SOURCE_DIR # top-level CMakeLists.txt directory CMAKE_BINARY_DIR # build tree directory CMAKE_CURRENT_SOURCE_DIR # current CMakeLists.txt directory CMAKE_CURRENT_BINARY_DIR # current build directory PROJECT_NAME # project name from project() ``` Pass variables from the command line: ```bash cmake -DCMAKE_BUILD_TYPE=Release .. cmake -DMY_OPTION=ON -DVERSION=2.0 .. ``` Use variables to avoid repetition, configure builds conditionally, and parameterize paths. They're the primary way to make CMake flexible across different environments and user preferences.