0

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) 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.