Skip to content

Instantly share code, notes, and snippets.

@OrangeTide
Created March 30, 2019 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OrangeTide/a09a8572e9d1260dac945536d509125c to your computer and use it in GitHub Desktop.
Save OrangeTide/a09a8572e9d1260dac945536d509125c to your computer and use it in GitHub Desktop.
Primitive modular Makefile
all ::
clean :: ; $(RM) $(TARGETS) $(foreach t,$(TARGETS),$(OBJS_$t))
.PHONY : all clean
## Configuration
CFLAGS = -Wall -W -O2 -g
# a really aggressive strip-all for ARM
STRIP = strip -s -w -R .comment\* -R .note\* -R .gnu.hash -R .ARM.exidx -R .ARM.attributes
## Rules
%.o : %.c
$(COMPILE.c) $(if $(PKGS),$(shell pkg-config --cflags $(PKGS))) $(OUTPUT_OPTION) $<
% : %.o
$(LINK.o) $(if $(PKGS),$(shell pkg-config --libs $(PKGS))) $^ $(LOADLIBES) $(LDLIBS) -o $@
$(STRIP) $@
% : %.c
$(LINK.c) $(if $(PKGS),$(shell pkg-config --cflags --libs $(PKGS))) $^ $(LOADLIBES) $(LDLIBS) -o $@
$(STRIP) $@
# each project is defined by a .mk file
include $(wildcard *.mk)
# establish default rule
all :: $(TARGETS)
TARGETS += hello
OBJS_hello = hello.o util.o
hello : $(OBJS_hello)
hello : PKGS += x11 xext
hello : LDLIBS += -lm
TARGET += testprog
OBJS_testprog = testprog.o util.o
testprog : $(OBJS_testprog)
testprog : CFLAGS += -DTESTMODE=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment