2012-02-02 44 views
2

我明白,「顯式」模式規則在其先決條件可以生效時將優先於其隱式對應模式。確保存在模式規則的先決條件

all: src/foo.o 

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

如果存在「my_header.h」的拼寫錯誤,則%.o的隱式規則優先。不僅我的食譜不會被執行,而且觸摸先決條件也不會觸發規則。其實這是我感興趣的第二點。

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'

第一條消息是在這裏,因爲我沒有打擾$(filter)ing SET_OF_FILES。我不知道如何解決接下來的警告,對於任何評論者來說,這意味着「有些事情是錯誤的」。

是否有另一種(更優雅的)方法來驗證先決條件實際上是否可行,以避免丟棄顯式模式規則?

(使用GNU使3.79.1的win32)

+0

你的 「重疊」 例如沒有按對我來說沒有意義,因爲第一個子句應該指定模式規則適用的目標文件。 你將兩個目標結合在一起:這就是說每個目標都應該使用兩個規則。 – ewindes 2017-07-12 22:16:12

回答

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 
+0

我認爲過濾SET_OF_FILES確實是我的問題的罪魁禍首。在你的解決方案中,make可能會發出一個錯誤,因爲target'all'需要目標'prereqs',而這又需要'Makefile'和'my_header.h'。它甚至不檢查兩個過濾的規則,這是應該驗證的規則。 – TallFurryMan 2017-08-03 12:53:36