as I understood, these variables are used in case project consist of sub directories e.g. every sub directory further have CMakeLists.txt files.
CMAKE_CURRENT_SOURCE_DIR
refers to the path of source director under process ? and
CMAKE_SOURCE_DIR
refers to top most source directory ? I am not sure about it.
CMAKE_SOURCE_DIR
is where cmake was originally invoked, and CMAKE_CURRENT_SOURCE_DIR
is where cmake is currently working. For instance, if you use add_subdirectory
command to include a dependency to your project, the dependency will have its own CMAKE_CURRENT_SOURCE_DIR
but CMAKE_SOURCE_DIR
will remain the same.
Expanding on Some programmer dude comment, imagine you have the following three CMakeFiles on different directories on your project
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
message("root dir CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("root dir CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")
add_subdirectory(sub1)
add_subdirectory(sub2)
sub1/CMakeLists.txt
message("sub1 dir CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("sub1 dir CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")
sub2/CMakeLists.txt
message("sub2 dir CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("sub2 dir CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")
Running cmake .
on the root directory gives me this output
root dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests
root dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests
sub1 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests
sub1 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
sub2 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests
sub2 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub2
Now if I run cmake .
on the sub1 directory I get the same value for both variables:
sub1 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
sub1 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With