dbotha
dbotha

Reputation: 1703

Makefile variable expansion

The following is a contrived example Makefile illustrating a problem that I'm having.

release: TYPE := release
FILE = main.cpp
OBJDIR = dist/$(TYPE)
OBJS = $(patsubst %.cpp,$(OBJDIR)/%.o,$(FILE))

release: $(OBJS)
    @echo "just created: " $(OBJS)

%.o:
    @echo "create $@"

When I run 'make release' the output is:

create dist//main.o
just created:  dist/release/main.o

How can I ensure that the $(OBJS) dependency of release target is expanded to dist/release/main.o and not dist//main.o . Also what is the reason for it expanding to dist//main.o?

Upvotes: 2

Views: 1125

Answers (1)

eriktous
eriktous

Reputation: 6659

The reason for it expanding to dist//main.o is that TYPE is a target-specific variable. The value of this kind of variable is only available within the context of a target's recipe (and in other target-specific assignments.
This means the value of TYPE is empty in the prerequisites for that rule.

Upvotes: 2

Related Questions