makefile 自动推导的坑-文件更新

    有 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值