zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Makefile 书写规则 规则举例 规则的语法 通配符 文件搜寻 伪目标 多目标 静态模式 自动生成依赖性

文件规则静态自动模式 生成 语法 目标
2023-09-14 09:06:15 时间

      规则:依赖关系,生成目标的方法
      Makefile 只有一个最终目标

规则例子

# main 模块
# main.o 依赖于 main.c 和 main.h 的文件
main.o : main.c main.h
    cc -c -g main.c # tab

 

规则的语法

# targets: 文件名,可空格分开,通配符
# command :命令行
targets : prerequisites
    command    # tab
    ...
# command与prerequisites在一行 分号做为分隔
targets : prerequisites ; command
    command
    ...


通配符

      make 通配符: “ * ” “ ? ” “ [...] ”

     “ *.c  ”:所有后缀为 c 的文件
例子:

clean:
    rm -f *.o
# 目标 text依赖于所有的[.c]文件
# “$?”: 自动化变量
text : *.c
lpr -p $?
touch text
# objects值: “ *.o ”
objects = *.o
# objects : 所有[.o]的文件名的集合
objects := $(wildcard *.o)


文件搜寻

     给 make路径,让 make 自动找

     make 会在当前的目录中找 依赖文件和 目标文件,并在 特殊变量“  VPATH ”目录中找文件
 

# 定义“ sys ”和“ ../mytext ”
# 目录由' : '分隔
VPATH = sys:../mytext

    另一种设置文件搜索路径的方法:

# 符合模式<pattern>的文件指定搜索目录<directories>
# pattern : 搜索的文件集(% 匹配n个字符)
# directories :搜索目录
vpath <pattern> <directories>
# 清除符合模式<pattern>的文件的搜索目录
vpath <pattern>
# 清除所有已被设置好了的文件搜索目录
vpath
# 先“mytext”目录,后“text”,最后“tar”目录
vpath %.c mytext
vpath text
vpath %.c tar
#上述一样
vpath %.c mytext:text
vpath %.c tar


伪目标

      

# make clean” 使用该目标
cleam:
    rm *.o text
# 指明目标的伪指令
.PHONY : clean
# make clean

.PHONY : clean
clean:
    rm *.o temp

 

# 声明 all为伪指令
all : test temp
.PHONY : all

test : test.o 
    cc -o test test.o

temp : temp.o
    cc -o temp temp.o 

多目标

    

# $@ : 所有的目标的集合
bigoutput littleoutput : test.g
generate text.g -$(subst output,, $@) > $@

#与上例相同
bigoutput : test.g
generate test.g -big > bigoutput
littleoutput : test.g
generate test.g -little > littleoutput


静态模式

# targets :目标文件
# rarget-pattern : 目标集模式
# prereq-patterns:目标的依赖模式
<targets...>:<target-pattern...>:<prereq-patterns...>
<commands>
...


自动生成依赖性

       

# main.c中:“ #include "defs.h" ”
main.o : main.c defs.h