我该怎么做这个makefile?

时间:2011-11-22 02:01:05

标签: 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

我尝试了一些方法,但都失败了。 我应该怎么做?谢谢

1 个答案:

答案 0 :(得分: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
相关问题