views:

147

answers:

1

In my makefile (rather simplified below) I have a target-specific variable that needs to influence a recursively expanded variable, but it's not doing so - leaving me sitting here scratching my head:

NAME            = MyProg
OBJECTS         = $(OD)/main.o
RD              = Release
OD              = Foo

all: OD = $(RD)
all: $(OD) $(OD)/$(NAME).elf

$(OD):
    mkdir $(OD)

$(OD)/$(NAME).elf: $(OBJECTS)
    $(CC) $(LDFLAGS) -o "$@" $^

$(OD)/%.o: %.c
    $(CC) $(CFLAGS) $(INCLUDES) -Wa,-a="$(OD)/$*.lst" -o "$@" "$<"

The command that actually gets executed is as follows:

gcc -O2 -Wall -Wstrict-prototypes -c -Wa,-a="Release/main.lst" -o "Foo/main.o" "main.c"

$(OD) is correctly evaluated in the ".lst" expression, but incorrectly by $(OBJECTS). It must be that $(OBJECTS) is evaluating $(OD) outside my rule... so is it possible to correct this behavior? Where am I making the mistake?

+1  A: 

The Make manual says this about target-specific variables

"these values are only available within the context of a target's command script (and in other target-specific assignments). " 1

and I guess this is what you are seeing, the target-specific variable is not expanded in targets themselves.

What is it you're trying to accomplish?

Per Ekman
I would like $(OBJECTS) to expand to "Release/main.o" instead of "Foo/main.o"; I had thought that variables were lazily evaluated, so that this is how it would be expanded.
Nate
I understand that, but what I mean to ask is why Release must be target specific. What is the scenario when it has some other value?Do you have a "debug" target and want to put the debug object filesin a Debug directory?
Per Ekman
yes - as I said, the posted makefile was very much over-simplified.
Nate