CMake Variables

Variables that Provide Information

  • CMAKE_CURRENT_SOURCE_DIR The path to the source directory currently being processed.

  • CMAKE_CURRENT_BINARY_DIR The path to the binary directory currently being processed.

  • CMAKE_SOURCE_DIR The path to the top level of the source tree.

  • CMAKE_BINARY_DIR The path to the binary directory currently being processed.

    • for an in-source build, this would be the same as CMAKE_SOURCE_DIR

    • for an out-of-source build, this would be at ./build folder

  • PROJECT_SOURCE_DIR Top level source directory for the current project.

  • PROJECT_BINARY_DIR Full path to build directory for project.

  • <PROJECT-NAME>_SOURCE_DIR Top level source directory for the named project.

  • <PROJECT-NAME>_BINARY_DIR Top level binary directory for the named project.

  • PROJECT_VERSION Value given to the VERSION option of the most recent call to the project() command, if any.

  • CMAKE_VERSION

  • CMAKE_COMMAND The full path to the cmake executable.

  • CMAKE_GENERATOR The generator used to build the project.

# ~/catkin_ws/src/example_pkg
message(${CMAKE_CURRENT_SOURCE_DIR})
message(${CMAKE_SOURCE_DIR})
message(${PROJECT_SOURCE_DIR})

# ~/catkin_ws/build/example_pkg
message(${CMAKE_CURRENT_BINARY_DIR})
message(${CMAKE_BINARY_DIR})
message(${PROJECT_BINARY_DIR})

Variables that Change Behavior

  • CMAKE_BUILD_TYPE Specifies the build type on single-configuration generators.

    • Can be set to Debug, Release, RelWithDebInfo, MinSizeRel, ...

  • CMAKE_CONFIGURATION_TYPES Specifies the available build types on multi-config generators.

  • CMAKE_MODULE_PATH List of directories to search for CMake modules.

    • Commands like include() and find_package() search for files in directories listed by this variable before checking the default modules that come with CMake.

  • CMAKE_INSTALL_PREFIX Install directory used by install.

    • If “make install” is invoked or INSTALL is built, this directory is prepended onto all install directories.

    • This variable defaults to /usr/local on UNIX and c:/Program Files on Windows.

CMAKE_XXX_PATH

By default it is empty, it is intended to be set by the project.

  • CMAKE_PREFIX_PATH Path used for searching by FIND_XXX(), with appropriate suffixes added.

    • a.k.a. CATKIN_DEVEL_PREFIX in earlier CMake versions; set to catkin_ws/devel for example.

    • It contains the “base” directories, the FIND_XXX() commands append appropriate subdirectories to the base directories.

      • FIND_PROGRAM() adds /bin to each of the directories in the path,

      • FIND_LIBRARY() appends /lib to each of the directories, and

      • FIND_FILE() and FIND_PATH() append /include.

  • CMAKE_IGNORE_PATH Path to be ignored by FIND_XXX() commands.

    • This is useful in cross-compiled environments where some system directories contain incompatible but possibly linkable libraries.

  • CMAKE_INCLUDE_PATH Path used for searching by FIND_FILE() and FIND_PATH().

  • CMAKE_LIBRARY_PATH Path used for searching by FIND_LIBRARY().

  • CMAKE_PROGRAM_PATH Path used for searching by FIND_PROGRAM().

CMAKE_SYSTEM_XXX_PATH

By default it contains the standard directories for the current system. It is NOT intended to be modified by the project.

  • CMAKE_SYSTEM_IGNORE_PATH

  • CMAKE_SYSTEM_INCLUDE_PATH

  • CMAKE_SYSTEM_LIBRARY_PATH

  • CMAKE_SYSTEM_PREFIX_PATH

  • CMAKE_SYSTEM_PROGRAM_PATH

message("${CMAKE_BUILD_TYPE}")  # add "" to prevent error from empty variable
message("${CMAKE_CONFIGURATION_TYPES}")
message("${CMAKE_MODULE_PATH}")
message("${CMAKE_INSTALL_PREFIX}")

Variables that Describe the System

  • CMAKE_HOST_SYSTEM

  • CMAKE_SYSTEM_NAME

  • UNIX or CMAKE_HOST_UNIX

  • WIN32 or CMAKE_HOST_WIN32

  • MSVC, MSVC_VERSION

  • ENV: Access environment variables.

    • Use the syntax $ENV{VAR} to read environment variable VAR.

    • See also the set() command to set ENV{VAR}.

Variables that Control the Build

  • EXECUTABLE_OUTPUT_PATH

  • LIBRARY_OUTPUT_PATH

  • CMAKE_RUNTIME_OUTPUT_DIRECTORY Where to put all the RUNTIME targets when built.

    • This variable is used to initialize the RUNTIME_OUTPUT_DIRECTORY property on all the targets.

    • See that target property for additional information.

Variables for Languages

  • CMAKE_<LANG>_COMPILER The full path to the compiler for LANG.

    • CMAKE_CXX_COMPILER, CMAKE_C_COMPILER

  • CMAKE_<LANG>_FLAGS Flags for all build types.

    • CMAKE_CXX_FLAGS, CMAKE_C_FLAGS

    • CMAKE_<LANG>_FLAGS_RELEASE Flags for Release build type or configuration.

  • CMAKE_CXX_STANDARD New in version 3.1. Default value for CXX_STANDARD property of targets.

  • CMAKE_CUDA_STANDARDNew in version 3.8. Default value for CUDA_STANDARD property of targets.

Last updated