天天看點

makefile的收攏式表達依賴關系

在上篇《make指令的自動推導》中,我們利用make的自動推導簡化了makefile:

OBJECTS = main.o hello.o
app:$(OBJECTS)
        gcc -o app $(OBJECTS) 
main.o:hello.h
hello.o:hello.h
clean:
        rm app $(OBJECTS)      

我們可以看到下面的内容,hello.h顯得有點重複了:

main.o:hello.h
hello.o:hello.h      

我們可以把依賴hello.h的關系合在一起:

main.o hello.o:hello.h      

又因為main.o hello.o已用OBJECTS變量表示,是以我們還可以寫成:

$(OBJECTS):hello.h      
OBJECTS = main.o hello.o
app:$(OBJECTS)
        gcc -o app $(OBJECTS) 
$(OBJECTS):hello.h
clean:
        rm app $(OBJECTS)      

繼續閱讀