Skip to content

Instantly share code, notes, and snippets.

@nealian
Created October 24, 2016 19:54
Show Gist options
  • Save nealian/caca7d5a189ede579e2402a75e01fd65 to your computer and use it in GitHub Desktop.
Save nealian/caca7d5a189ede579e2402a75e01fd65 to your computer and use it in GitHub Desktop.
My old generic C Makefile
EXCL =
#Include every .c file that will become an object in EXCL, as well as any other .c files to not make in this dir
C_FILES = $(filter-out $(EXCL),$(wildcard *.c))
PROGRAMS = $(C_FILES:.c=)
# For each main program named <main>.c, make a variable <main>_OBJS with .o files to add,
# and <main>_LIBS with libraries to link with -l
# Everything after this is generic
CC = gcc
CFLAGS = -Wall -ggdb
.PHONY: all
all: $(PROGRAMS)
define PROGRAM_template =
$(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) $(1).c
ALL_OBJS += $(filter-out $(ALL_OBJS),$$($(1)_OBJS))
endef
$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
$(PROGRAMS):
$(CC) $(CFLAGS) $^ $(LDLIBS) -o $@
%.o : %.c
$(CC) $(CFLAGS) -c $<
clean:
$(RM) $(ALL_OBJS) $(PROGRAMS) *~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment