Reputation: 1
I am trying to work out how to write a makefile template that handles multiple c++ extensions, for a given list of source files - but don't want to have separate rules for each possible extension. Is this possible?
e.g.
TMP_CPPEXTS := cpp c++
TMP_OBJDIR := ~/objfiles
TMP_SOURCEFILES := foo.cpp bar.c++
TMP_BASENAMES := $(basename $(TMP_SOURCEFILES))
TMP_OBJFILES := $(addprefix $(TMP_OBJDIR)/,$(addsuffix .o,$(notdir $(TMP_BASENAMES))))
TMP_DEPFILES :=$(addprefix $(TMP_OBJDIR)/,$(addsuffix .d,$(notdir $(TMP_BASENAMES))))
BD_EXTFROMBASE = $(strip $(foreach TMP_EXT,$(2),$(foreach TMP_NAME,$(1),$(if $(filter $(addsuffix .$(TMP_EXT),$(TMP_NAME)),$(TMP_SOURCEFILES)),$(EXT)))))
$(TMP_OBJFILES) : $(TMP_OBJDIR)/%.o : %.$(call BD_EXTFROMBASE,$*,$(TMP_CPPEXTS))
g++ -c $< -o $@
$(TMP_DEPFILES) : $(TMP_OBJDIR)/%.d : %.$(call BD_EXTFROMBASE,$*,$(TMP_CPPEXTS))
g++ -m $< -o $@
I have verified that TMP_OBJFILES contains the expected object files, but cant seem to get the rule itself to work? Any suggestions other than use separate rules/macros?
Upvotes: 0
Views: 144
Reputation: 99144
How about this:
define templ
$(TMP_OBJDIR)/%.o: %.$(1)
g++ -c $$< -o $$@
endef
$(foreach ext,$(TMP_CPPEXTS),$(eval $(call templ,$(ext))))
Upvotes: 2