据我了解,这些变量用于项目由子目录组成的情况,例如每个子目录还具有CMakeLists.txt文件。
CMAKE_CURRENT_SOURCE_DIR
是指正在处理的源主管的路径? 和
CMAKE_SOURCE_DIR
是指最高级的源目录? 我不确定。
答案 0 :(得分:4)
CMAKE_SOURCE_DIR
是cmake最初被调用的地方,CMAKE_CURRENT_SOURCE_DIR
是cmake当前正在工作的地方。例如,如果您使用add_subdirectory
命令将对项目的依赖项包括在内,则该依赖项将具有自己的CMAKE_CURRENT_SOURCE_DIR
,但CMAKE_SOURCE_DIR
将保持不变。
扩展一些程序员的评论,想象一下您在项目的不同目录中有以下三个CMakeFiles
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}")
在 root 目录上运行cmake .
会给我输出
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
现在,如果我在 sub1 目录上运行cmake .
,则两个变量的值相同:
sub1 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
sub1 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub1