Skip to content

Instantly share code, notes, and snippets.

@aprell
Created October 9, 2012 12:14
Show Gist options
  • Save aprell/3858449 to your computer and use it in GitHub Desktop.
Save aprell/3858449 to your computer and use it in GitHub Desktop.
Makefile rule generation
# Rule generation
# Project must define PROGS, SRCS, and *_SRCS to specify targets and prerequisites
OBJS = $(SRCS:.c=.o)
define RULE_template
$(1): $$($(subst -,_,$(1))_SRCS:.c=.o) $$($(subst -,_,$(1))_PREREQS)
$$(CC) -o $$@ $$(filter %.o,$$^) $$(LDFLAGS) $$(IMPORTS) $$($(subst -,_,$(1))_LIBS:%=-l%)
endef
# Default clean target
define RULE_clean
clean:
rm -f $(1) $$(OBJS) $$(DEPS)
endef
all: $(PROGS)
$(foreach prog,$(PROGS),$(eval $(call RULE_template,$(prog))))
$(eval $(call RULE_clean,$(PROGS)))
.PHONY: all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment