有 a.c, makefile 两个文件,使用自动推导如下
makefile:
objs = a.o
all: $(objs)
gcc -o all $(objs)
.PHONY: clean
clean:
rm all $(objs)
第一次 make 后,会生成 all 的执行文件和 a.o 到中间文件;再次 make 会提示 all 过时了,这是可以理解的。
如果对 a.c 进行了修改,再 make 下,会发现, 还是提示 all 过时了。
==================================================================
targets: prerequisites
recipe
...
gnu make 中 原文:
The criterion for being out of date is specified in terms of the prerequisites, which
consist of file names separated by spaces.====================================================================
过时时间由 prerequisites 指定的,看到这里我们再分析上面 makefile 问题
a.o 显然是自动推导生成的,而 a.o 的 prerequisites 为 null,说明只有当 a.o 不存在时,会生成 a.o;
当 a.o 存在时,不会更新 a.o了。这就是第一次 make 生成,第二次 make 过时的原因了。
知道这一点后,为了能够对源文件修改后重新生成 .o 的文件,我们怎么修改呢?
方法一:
用 make clean 指令后,再 make
方法二:
在自动推导中显式指明依赖关系, a.o: a.c
objs = a.o
all: $(objs)
gcc -o all $(objs)
a.o: a.c
.PHONY: clean
clean:
rm all $(objs)