官方的解释:
PROJECT_BINARY_DIR
contains the full path to the top level directory of your build tree
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
个人理解和用demo进行了测试,才大致理解。
举例:
目录结构:
CMakeLists.txt
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project(hello)
# Add an executable
add_executable(hello main.c)
message(STATUS "PROJECT_BINARY_DIR: ${PROJECT_BINARY_DIR}")
message(STATUS "PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
main.c
#include <stdio.h>
int main()
{
printf("Hello CMake!\n");
return 0;
}
cd build/
cmake ..
-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- PROJECT_BINARY_DIR: /home/pi/Documents/learn/helloProject/build
-- PROJECT_SOURCE_DIR: /home/pi/Documents/learn/helloProject
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/Documents/learn/helloProject/build
PROJECT_BINARY_DIR: /home/pi/Documents/learn/helloProject/build
PROJECT_SOURCE_DIR: /home/pi/Documents/learn/helloProject
很明显,已经看出区别了
PROJECT_BINARY_DIR是CMake生成一系列文件的目录,包括MakeFile等文件,如果你是让他们生成在build目录中就是/home/pi/Documents/learn/helloProject/build
如果是让他们生成在helloProject目录(cmake . 命令)
那么这个变量的值就是/home/pi/Documents/learn/helloProject
PROJECT_SOURCE_DIR是顶级的CMakeLists.txt所在的目录,也就是/home/pi/Documents/learn/helloProject目录
如果有子目录,子目录中也有CMakeLists.txt,那这两个目录还会是这样吗?
仍然是这样,不会改变这两个变量的结果。