CptanPanic
CptanPanic

Reputation: 639

Cannot get make to use variable I set inside of rule

I am trying to fix a problem with a makefile. If you see below, I am setting the value of SUBDIR inside the rule, but it is not set when it gets to the next line. I have verified that the subst command is correct using output warnings, but it seems that the command on the next line is generated before the line above is executed. Is this possible? What am I doing wrong?

$(CppObj):$(OBJPATH)/%.$(OBJ_EXT): $(CPPPATH)/%.cpp
@$(MKDIR) $(OBJPATH)
@$(RM) $@
SUBDIR = $(subst $(OBJPATH),,$(@D))
$(CC) $(C++FLAGS)  $(CCOMP_ONLY_FLAG)  $<  $(COBJ_NAME_FLAG)$(OBJPATH)$(SUBDIR)$(@F) 

Upvotes: 0

Views: 155

Answers (1)

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25533

Recipes are actually shell commands. You can't set a Make variable inside them (well, except for using eval function).

But it's not a problem to set it outside the rule and refer it in the recipe as usual:

SUBDIR = $(subst $(OBJPATH),,$(@D))

$(CppObj):$(OBJPATH)/%.$(OBJ_EXT): $(CPPPATH)/%.cpp
    @$(MKDIR) $(OBJPATH)
    @$(RM) $@
    $(CC) $(C++FLAGS)  $(CCOMP_ONLY_FLAG)  $<  $(COBJ_NAME_FLAG)$(OBJPATH)$(SUBDIR)$(@F)

This will work fine as far as SUBDIR is recursively expanded variable, it's just a macro in fact.

Upvotes: 1

Related Questions