目录结构
.
├── build
├── CMakeLists.txt
├── main.cpp
└── sub_project
├── CMakeLists.txt
└── sub.cpp
cmake_minimum_required(VERSION 3.0.0)
project(main VERSION 0.1.0) # 会创建一个PROJECT_NAME变量,并赋值为main
message("CMAKE_ROOT=${CMAKE_ROOT}") # /usr/local/share/cmake-3.15
message("CMAKE_COMMAND=${CMAKE_COMMAND}") # /usr/local/bin/cmake
# 如果在build文件夹中编译,则为 workspaceFolder/build,否则等价于CMAKE_SOURCE_DIR, 对所有project一致
message("CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}") # /workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/build
# 顶层CmakeLists.txt的路径,对所有project一致
message("CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR}") # CMAKE_SOURCE_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables
# 当前处理的CMakeLists.txt的路径,主项目和子项目不同
message("CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
# CMAKE_CURRENT_SOURCE_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables
# CMAKE_CURRENT_SOURCE_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/sub_project
# 如果在build文件夹中编译,则为 workspaceFolder/build,否则等价于CMAKE_CURRENT_SOURCE_DIR
message("CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}")
# CMAKE_CURRENT_BINARY_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/build
# CMAKE_CURRENT_BINARY_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/build/sub_project
# 第一次调用project的项目名称,一般为顶层项目名称
message("CMAKE_PROJECT_NAME=${CMAKE_PROJECT_NAME}") # main
message("PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}")
# PROJECT_SOURCE_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables
# PROJECT_SOURCE_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/sub_project
message("PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}")
# PROJECT_BINARY_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/build
# PROJECT_BINARY_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/build/sub_project
message("CMAKE_CURRENT_LIST_FILE=${CMAKE_CURRENT_LIST_FILE}")
# CMAKE_CURRENT_LIST_FILE=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/CMakeLists.txt
# CMAKE_CURRENT_LIST_FILE=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/sub_project/CMakeLists.txt
message("CMAKE_CURRENT_LIST_DIR=${CMAKE_CURRENT_LIST_DIR}")
# CMAKE_CURRENT_LIST_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables
# CMAKE_CURRENT_LIST_DIR=/workspace_wjr/develop/tutorials/cmake_tutorials/04_cmake_variables/sub_project
message("CMAKE_CURRENT_LIST_LINE=${CMAKE_CURRENT_LIST_LINE}") # CMAKE_CURRENT_LIST_LINE=21
message("CMAKE_FILES_DIRECTORY=${CMAKE_FILES_DIRECTORY}") # CMAKE_FILES_DIRECTORY=/CMakeFiles
message("CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") # FIND_PACKAGE()或者INCLUDE()执行时,优先从CMAKE_MODULE_PATH中搜索.cmake文件
# SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/MyCMakeScripts) FIND_PACKAGE(HelloWorld)
add_subdirectory(sub_project)
add_executable(main main.cpp)