规则:
target:prerequisites
command
预定义常量:
$*
不包含扩展名的目标文件名称。
$+
所有的依赖文件,以空格分开,并以出现的先后为序,可能包含重复的依赖文件。
$<
第一个依赖文件的名称。
$?
所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚。
$@
目标的完整名称。
$^
所有的依赖文件,以空格分开,不包含重复的依赖文件。
$%
如果目标是归档成员,则该变量表示目标的归档成员名称。例如,如果目标名称为
mytarget.so(image.o),则 $@ 为 mytarget.so,而 $% 为 image.o。。
-放在命令开头,表示出错了继续执行下面的命令
伪目标
.phony: clean
clean:
-rm *.tmp
VPATH: search path for all prerequisites
vpath: the directive specifies a search path for file names that match a particular pattern
vpath %.c directory
静态模式:
targets: target-pattern: prereq-patters
objects=foo.o bar.o
$(objects):%.o:%.c
$(CC) -c $(CFLAGS) $< -o $@
常用函数:
字符串替换
$(subst <from>,<to>,<text>)
模式字符串替换
$(patsubst <pattern>,<replacement>,<text>)
去掉空格
$(strip <string>)
查找字符串函数
$(findstring <find>,<in>)
过滤函数
$(filter <pattern...>,<text>)
排序函数
$(sort <list>)
取前缀函数
$(basename <names...>)
取后缀函数
$(suffix <names...>)
连接函数
$(join <list1>,<list2>)
immediate = deferred
immediate ?= deferred
immediate := immediate
immediate += deferred or immediate
For include file in %.c, the 'gcc -M' output the dependancies.
%.d: %.c
@set -e; rm -f $@; \ #nomally, make output the command before it is executed; when a line starts with a '@', the echoing of the line is supressed.
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;
# rm -f $@.$$$$
-include $(sources:.c=.d) # '-' 出错继续执行,不报错;否则报错‘找不到文件’。 被include的文件也被当作目标来尝试,可以匹配规则'%d:%c'。 用';'隔开的命令为子命令,在同一个进程中执行。