CMake PROJECT_BINARY_DIR和PROJECT_SOURCE_DIR的理解

官方的解释:

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,那这两个目录还会是这样吗?

仍然是这样,不会改变这两个变量的结果。

参考:
CMake PROJECT_BINARY_DIR和PROJECT_SOURCE_DIR区别

PROJECT_SOURCE_DIR

PROJECT_BINARY_DIR

  • 20
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值