vv1133
vv1133

Reputation: 739

how should I do for this 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 *~

the output of this makefile is:

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

but I want the output like this:

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

I tried some methods but all failed. how should I do? thank you

Upvotes: 0

Views: 71

Answers (1)

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25483

Try target-specific variables:

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

Upvotes: 3

Related Questions