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.
https://cmake.org/cmake/help/v3.5/variable/CMAKE_BINARY_DIR.html
CMAKE_BINARY_DIR
The path to the top level of the build tree.
This is the full path to the top level of the current CMake build tree. For an in-source build, this would be the same as CMAKE_SOURCE_DIR
.
When run in -P script mode, CMake sets the variables CMAKE_BINARY_DIR
, CMAKE_SOURCE_DIR
, CMAKE_CURRENT_BINARY_DIR
and CMAKE_CURRENT_SOURCE_DIR
to the current working directory.
CMAKE_SOURCE_DIR,PROJECT_SOURCE_DIR,_SOURCE_DIR:
这三个变量内容一致,都指的是工程的顶级目录。
CMAKE_CURRENT_BINARY_DIR:外部编译时,指的是target目录,内部编译时,指的是顶级目录
CMAKE_CURRENT_SOURCE_DIR:CMakeList.txt所在的目录
CMAKE_CURRENT_LIST_DIR:CMakeList.txt的完整路径
CMAKE_CURRENT_LIST_LINE:当前所在的行
CMAKE_MODULE_PATH:如果工程复杂,可能需要编写一些cmake模块,这里通过SET指定这个变量
LIBRARY_OUTPUT_DIR,BINARY_OUTPUT_DIR:库和可执行的最终存放目录