标头更改时重建目标文件 [英] Rebuilding object files when a header changes

查看:59
本文介绍了标头更改时重建目标文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Makefile中有以下规则:

I have the following rules in my Makefile:

%.o:        $(HFILES)

%.o:        %.c
    $(CC) $(CFLAGS) $*.c

其中 HFILES 包含我项目的所有标头.

where HFILES contains all headers of my project.

问题在于,当头文件按预期更改时,这不会重建目标文件.为什么第一行不将标头添加到目标文件的前提条件中?

The Problem is that this does not rebuild the object files when a header changes as intended. Why does the first line not add the headers to the prerequisites of the object files?

推荐答案

因为这不是模式规则的工作方式.模式规则的文档说,当您创建一个没有规则的模式规则时,该规则不会取消(即删除它).

Because that's not how pattern rules work. The documentation for pattern rules says that when you create a pattern rule with no recipe that cancels the pattern rule (that is, deletes it).

由于您的第一行正在创建具有目标%.o 和先决条件 $(HFILES)但没有配方的模式规则,因此该行仅取消了一个模式规则(反正不存在).

Since your first line is creating a pattern rule with a target %.o and prerequisites $(HFILES) but no recipe, that line simply cancels a pattern rule (which doesn't exist anyway).

您可以写:

%.o: %.c $(HFILES)
         $(CC) $(CFLAGS) -c -o $@ $<

(您不应在 CFLAGS 变量中放置 -c 标志).

(you shouldn't put the -c flag in your CFLAGS variable).

请注意,当然,这意味着如果 HFILES 中的任何头文件发生更改,使用此模式的 ALL .o 文件将被重建.

Be aware that, of course, this means that if ANY header file in HFILES changes, ALL .o files that use this pattern will be rebuilt.

这篇关于标头更改时重建目标文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆