我应该如何为这个makefile做些什么?

问题描述:

$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 –

尝试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