确保模式规则存在先决条件

时间:2012-02-02 16:05:28

标签: makefile gnu-make

据我所知,“显式”模式规则在其先决条件可以优先于其隐含对应规则。

all: src/foo.o

src/%.o: makefile my_haeder.h src/%.c
    echo Do something with those source files

如果“my_header.h”存在拼写错误,则%.o的隐式规则优先。不仅我的食谱不会被执行,而且触及先决条件也不会触发规则。实际上这是我感兴趣的第二点。

make文档使用静态模式规则提供验证:

SET_OF_FILES=src/foo.o

all: src/foo.o

$(SET_OF_FILES): src/%.o: makefile my_haeder.h src/%.c
    echo Do something with those source files

这导致:

gmake: *** No rule to make target `src/my_haeder.h', needed by `src/foo.o'.  Stop.

虽然规则较大,但这个解决方案很好,只要我不必添加一个可以重叠的规则:

SET_OF_FILES=src/foo.o src/subsrc/bar.o

all: src/foo.o

$(SET_OF_FILES): src/%.o: makefile my_header.h src/%.c
    echo Do something with those source files

$(SET_OF_FILES): src/subsrc/%.o: makefile my_header.h src/subsrc/%.c
    echo Do something with those other source files

结果是:

makefile:8: target `src/foo.o' doesn't match the target pattern
makefile:9: warning: overriding commands for target `src/foo.o'
makefile:6: warning: ignoring old commands for target `src/foo.o'
makefile:9: warning: overriding commands for target `src/subsrc/bar.o'
makefile:6: warning: ignoring old commands for target `src/subsrc/bar.o'

第一条消息在这里是因为我没有打扰$(过滤)SET_OF_FILES。我不知道如何解决下一个警告,对于任何评论者来说都意味着“出了问题”。

是否有另一种(更优雅的)方法来验证先决条件是否真的可行,以避免删除显式模式规则?

(使用GNU Make 3.79.1 win32)

1 个答案:

答案 0 :(得分:0)

添加单独的规则以检查您的先决条件

all: prereqs src/foo.o

prereqs: Makefile my_header.h

src/%.o: src/%.c Makefile my_header.h
    echo Do something with those source files

src/subsrc/%.o: src/subsrc/%.c Makefile my_header.h
    echo Do something with those other source files

会给你:

make 
make: *** No rule to make target 'my_header.h', needed by 'prereqs'.  Stop