Skip to content
Search

CMake:Variables

lastmod 2021-01-02created 2016-04-1919 edits

Lists the variables used in CMake.

Category

When a variable is not assigned

If a variable such as CMAKE_STATIC_LIBRARY_PREFIX has no value assigned, use it after calling the project() command.

List of variables

CMAKE_<LANG>_COMPILER_ID
Stackoverflow: In cmake, how can I test if the compiler is Clang?
CMake » 3.0.2 Documentation » cmake-variables(7) » CMAKE_<LANG>_COMPILER_ID
Gets the compiler ID (string). <span style="text-color:red;">(Compiler detection must be completed via enable_language().)
CXX_STANDARD
Stackoverflow: How to activate C++ 11 in CMake?
CMake » 3.1.3 Documentation » cmake-properties(7) » CXX_STANDARD
CMake » 3.1.3 Documentation » cmake-commands(7) » target_compile_features
CMAKE_PROGRAM_PATH
You can append to it like LIST(APPEND CMAKE_PROGRAM_PATH "c:/MyProject/Tools/mingw/bin/" ...).
The list of PATHs CMake uses to search for executables.
CMAKE_LIBRARY_PATH
The list of LIBRARY_PATHs CMake applies to the linker.
CMAKE_COMMAND
The cmake command, including the full path.
CMAKE_EXECUTABLE_SUFFIX
Executable file extension. (.exe on Win32)
CMAKE_STATIC_LIBRARY_PREFIX
Static library prefix. (lib on Unix)
CMAKE_STATIC_LIBRARY_SUFFIX
Static library suffix. (.a on Unix)
BUILD_SHARED_LIBS
If ON, builds shared libraries; if OFF, builds static libraries.
CMAKE_VERBOSE_MAKEFILE
If ON, prints verbose build messages. (make VERBOSE=1)
CMAKE_BUILD_TYPE
Build type (Debug, Release, RelWithDebInfo, MinSizeRel)
CMAKE_INSTALL_PREFIX
Sets the path to install to with the make install command.
CMAKE_INCLUDE_PATH
This is used when searching for include files. (e.g. using the FIND_PATH() command)
CMAKE_LIBRARY_PATH
This is used when searching for libraries. (e.g. using the FIND_LIBRARY() command)
CMAKE_PREFIX_PATH
(since CMake 2.6.0) This is used when searching for include files, binaries, or libraries using either the FIND_PACKAGE(), FIND_PATH(), FIND_PROGRAM(), or FIND_LIBRARY() commands.
CMAKE_PREFIX_PATH
???
CMAKE_C_FLAGS
CMAKE_CXX_FLAGS
Stackoverflow: cmake CFLAGS CXXFLAGS modification
CC & C++ command flags
CMAKE_CXX_COMPILER
CXX compiler.
PROJECT_SOURCE_DIR
The directory where CMakeLists.txt is located.
PROJECT_BINARY_DIR
The directory where the actual build takes place.
CMAKE_MACOSX_RPATH
CMake » 3.0.2 Documentation » cmake-properties(7) » MACOSX_RPATH
Whether to use rpaths on Mac OS X.
This variable is used to initialize the MACOSX_RPATH1 property on all targets.
CMAKE_INSTALL_NAME_DIR
Mac OS X directory name for installed targets.
CMAKE_INSTALL_NAME_DIR is used to initialize the INSTALL_NAME_DIR property on all targets. See that target property for more information.
CMAKE_CXX_EXTENSIONS
Compile Feature Requirements
Default value for CXX_EXTENSIONS property of targets.
CMAKE_CXX_STANDARD
Default value for CXX_STANDARD property of targets. Supported values are 98, 11 and 14.
CMAKE_CXX_STANDARD_REQUIRED
Boolean describing whether the value of CXX_STANDARD is a requirement.
CMAKE_POSITION_INDEPENDENT_CODE
If ON, applies -fPIC.
CMAKE_CURRENT_LIST_FILE
this is the full path to the listfile currently being processed.
CMAKE_CURRENT_LIST_DIR
(since 2.8.3) this is the directory of the listfile currently being processed.
CMAKE_CURRENT_LIST_LINE
this is linenumber where the variable is used.
CMAKE_CURRENT_SOURCE_DIR
this is the directory where the currently processed CMakeLists.txt is located in
CMAKE_CL_64
Set to true when using the 64 bit cl compiler from Microsoft.
CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS
Sets whether to export all symbols on Windows.

CMake Directory Variables

SOURCE_DIR Variables

These variables contain the paths to various source code directories. Note how every one of them refers to a directory that contains a CMakeLists.txt file.

CMAKE_SOURCE_DIR
The path to the top level of the source tree. This is the directory that contains the top-level CMakeLists.txt file. That is, this is the source directory you specify to the cmake command.
CMAKE_CURRENT_SOURCE_DIR
The path to the directory containing the CMakeLists.txt file that is currently being processed.
PROJECT_SOURCE_DIR
Top level source directory for the current project. Not every CMakeLists.txt file defines a project–this is the directory that contains the most recent CMakeLists.txt file that defined a project.
projectName_SOURCE_DIR
Source directory for the named project. This is the directory that contains the CMakeLists.txt file that contains the project(projectName) definition. Every CMakeLists.txt file need not define a project, but one reason to define a project is to create this variable so you can refer to its source files later, in other CMakeLists.txt files.

BINARY_DIR Variables

The build tree will contain a directory hierarchy corresponding to the hierarchy of directories in the source tree containing CMakeLists.txt files. Each of the following variables refers to a directory in the build tree corresponding to a source tree directory that contains a CMakeLists.txt file.

CMAKE_BINARY_DIR
The path to the top level of the build tree. This is the directory in which you ran the cmake command.
CMAKE_CURRENT_BINARY_DIR
The path to the binary directory currently being processed. When an add_subdirectory command is encountered in a CMakeLists.txt file, a corresponding directory is created in the build directory. This variable contains that subdirectory.
PROJECT_BINARY_DIR
Top level binary directory for the current project. Not every CMakeLists.txt file defines a project–this is the directory in the build tree that corresponds to the most recent CMakeLists.txt file that defined a project.
projectName_BINARY_DIR
Binary directory for the named project. This is the directory in the build tree that corresponds to the CMakeLists.txt file that contains a project(projectName) definition. Every CMakeLists.txt file need not define a project, but one reason to define a project is to create this variable so you can refer to its binary files later, in other CMakeLists.txt files.

Environment Variables

Environment variables can be set as follows.

set (ENV{PATH} "/home/martink")

Runtime output directories

As in Oleg's answer, I believe the correct variable to set is CMAKE_RUNTIME_OUTPUT_DIRECTORY. we use the following in our root CMakeLists.txt:

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

You can also specify the output directories on a per target basis:

set_target_properties( targets...
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

In both cases you can append _[CONFIG] to the variable/property name to make the output directory apply to a specific configuration (the standard values for config are DEBUG, RELEASE, MINSIZEREL and RELWITHDEBINFO).

Read cmake variable in bash script

To read a cmake variable in a Bash script:

NAME=`cmake -L -P CMakeLists.cmake | grep MAIN_NAME | sed -e 's/^MAIN_NAME.*=\\(.*\\)$/\\1/g'`

Favorite site

References


  1. When this property is set to true, the directory portion of the “install_name” field of shared libraries will be @rpath unless overridden by INSTALL_NAME_DIR. Runtime paths will also be embedded in binaries using this target and can be controlled by the INSTALL_RPATH target property. ↩

author Zer0 sitemap