Hello Headers
2026-01-24
Written by: xiaobin
| Variable | Info |
|---|---|
| CMAKE_CURRENT_SOURCE_DIR | The current source directory if using sub-projects and directories. |
| CMAKE_CURRENT_LIST_DIR | (since 2.8.3) this is the directory of the listfile currently being processed. |
| CMAKE_SOURCE_DIR | this is the directory which contains the top-level CMakeLists.txt, i.e. the top level source directory. |
| PROJECT_SOURCE_DIR | contains the full path to the root of your project source directory, i.e. to the nearest directory where CMakeLists.txt contains the PROJECT() command. |
The variables CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_LIST_DIR may refer to different directories for a CMake list file that is included by another file with the include command. E.g., if a CMakeLists.txt is present in a directory project and contains the following directive
include(src/CMakeLists.txt)
then while src/CMakeLists.txt is being processed CMAKE_CURRENT_LIST_DIR will refer to project/src whereas CMAKE_CURRENT_SOURCE_DIR still points to the outer directory project.
CMAKE_CURRENT_LIST_DIR comes in handy, when you need to locate resource files like template files or batch scripts that are located next to the CMakeLists.txt file currently being processed.
There is a difference between these variables. CMAKE_SOURCE_DIR does indeed refer to the folder where the top-level CMakeLists.txt is defined. However, PROJECT_SOURCE_DIR refers to the folder of the CMakeLists.txt containing the most recent project() command.
For example, say you have a top-level project called Outer and this contains a subdirectory with its own project called Inner. Outer’s CMakeLists.txt has:
project(Outer)
add_subdirectory(Inner)
and Inner’s:
project(Inner)
Then in both of these CMakeLists files, CMAKE_SOURCE_DIR will refer to Outer’s source dir. But while PROJECT_SOURCE_DIR for Outer is also this same dir, this is not the case for Inner. Inner’s PROJECT_SOURCE_DIR is the subdirectory containing its CMakeLists.txt.
This difference applies to all PROJECT_<var> vs CMAKE_<var> variables.
cmake_minimum_required(VERSION 4.2)
# Set the project name
project(mytest)
# Add an executable with the above sources
add_executable(mytest main.c hex2int.c)
target_include_directories(mytest
PRIVATE
${PROJECT_SOURCE_DIR}
)
source from 十六进制转十进制