duduru

Makefile中的include

include

首先看下面这段makefile

all:
        @echo "all"
include test1
test1:
        @echo "test1 create"
        @echo "A=1" > test1
test2:
        @echo "test2 create"

第一次make:
在这里插入图片描述

第2次make:
在这里插入图片描述

分析整个执行过程:

  • 读取Makefile,make并没有立即执行all目标,而是去执行include
    • include包含的文件存在(在当前目录下),替换(与C语言一样)
    • include包含的文件不存在,执行同名的目标
  • 重新读取Makefile,执行(这也称作remake)

所以第一次make的执行过程就变成:

  • 执行include,没有找到目录中有test文件
  • 执行同名目标test,其中一条命令创建了test文件
  • 重新读取Makefile,代码被替换
all:
        @echo "all"
A=1
test1:
        @echo "test1 create"
        @echo "A=1" > test1
test2:
        @echo "test2 create"
  • 执行all目标

扩展

  1. all里面可以使用变量A
  2. include包含的也可以是makefile,因为是原地替换,如果include的位置在all前面的话,执行的就是其他目标而非all目标

-include

-include与include唯一的不同:不报警告

posted on 2023-07-15 22:55  duduru  阅读(0)  评论(0)    收藏  举报  来源

导航