Introduction
CMake variables like CMAKE_SOURCE_DIR
are predefined variables in CMake that provide information about the build and source directory structure.
Directory Variables
CMAKE_SOURCE_DIR
:- Represents the top-level directory containing the root
CMakeLists.txt
file.
- Represents the top-level directory containing the root
CMAKE_BINARY_DIR
:- Refers to the top-level build directory where CMake generates the build files.
PROJECT_SOURCE_DIR
:- Refers to the source directory of the current project (set by
project()
inCMakeLists.txt
). - If there are multiple nested projects, this variable changes for each.
- Refers to the source directory of the current project (set by
PROJECT_BINARY_DIR
:- Refers to the binary directory of the current project.
- Similar to
CMAKE_BINARY_DIR
, but specific to the scope of the project.
CMAKE_CURRENT_SOURCE_DIR
:- Represents the directory containing the
CMakeLists.txt
file currently being processed. - Different from
CMAKE_SOURCE_DIR
, as it reflects the current directory in a hierarchical project.
- Represents the directory containing the
CMAKE_CURRENT_BINARY_DIR
:- Points to the build directory corresponding to the
CMAKE_CURRENT_SOURCE_DIR
.
- Points to the build directory corresponding to the
Summary
CMAKE_*
variables are global and refer to the top-level project.PROJECT_*
andCMAKE_CURRENT_*
variables adjust for subprojects and nested scopes.- These variables help maintain a clean separation between source and build directories, which is a core principle of CMake.
Tags ➡
C++