Rebuilding object files when a header changes

主宰稳场 提交于 2019-12-12 04:32:35

问题


I have the following rules in my Makefile:

%.o:        $(HFILES)

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

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?


回答1:


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

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

You can write:

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

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

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.



来源:https://stackoverflow.com/questions/40950455/rebuilding-object-files-when-a-header-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!