2011-11-22 63 views
0
$cat makefile 

AA:=3 

.PHONY:all 
all:test1.o test2.o 
    @echo all AA=${AA} 
    @gcc test1.o test2.o -o test 

test1.o:test1.c 
    @echo 1 AA=${AA} 
    @gcc -c test1.c -o test1.o 

AA:=2 
test2.o:test2.c 
    @echo 2 AA=${AA} 
    @gcc -c test2.c -o test2.o 

.PHONY:clean 
clean: 
    rm -f *.o 
    rm -f *~ 

此生成的文件的輸出是:我應該如何爲這個makefile做些什麼?

1 AA=2 
2 AA=2 
all AA=2 

,但我想這樣的輸出:

1 AA=3 
2 AA=2 
all AA=3 

我嘗試了一些方法,但都失敗了。 我該怎麼辦?謝謝

+0

我覺得你有在錯誤的道路 一個makefile不一定執行以相同的順序「條件」,其中提出的整體概念,也依賴於依賴關係的最後修改日期和基本上所有規則 所以這裏是一個教程,我希望應該幫助: http://www.gnu.org/software/make/manual/html_node/Simple-Makefile.html#簡單的Makefile –

回答

3

嘗試target-specific變量:

all: AA:=3 
all:test1.o test2.o 
    @echo all AA=${AA} 
    @gcc test1.o test2.o -o test 

test2.o: AA:=2 
test2.o:test2.c 
    @echo 2 AA=${AA} 
    @gcc -c test2.c -o test2.o