Skip to content

Instantly share code, notes, and snippets.

Last active July 28, 2020 04:02
What would you like to do?
Makefile template, usage: `make all_src_files[ debug=mode]`. From
# use bash as the default shell
SHELL := bash
# ensures each Make recipe is ran as one single shell session, rather than one new shell per line
# use bash strict mode.
.SHELLFLAGS := -eu -o pipefail -c
# remove target files when Make file failed
# warning when referring the undefined variables
MAKEFLAGS += --warn-undefined-variables
# disable the builtin rules. Only do what I tell you!
MAKEFLAGS += --no-builtin-rules
# replace tabs
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
CC := gcc
CFLAGS := -Wall -Werror
ifeq ($(mode), debug)
CFLAGS += -g
OS := $(shell uname -s)
ifeq ($(OS), Linux)
LIBS += -pthread
SRCS := all_src_files.c
OBJS := ${SRCS:c=o}
PROGS := ${SRCS:.c=}
.PHONY: all clean
# Default - top level rule is what gets ran when you run just `make`
all: ${PROGS}
${PROGS} : % : %.o Makefile
> ${CC} $< -o $@ ${LIBS}
%.o: %.c Makefile
> ${CC} ${CFLAGS} -c $<
# Clean up
> rm -f ${OBJS}
> rm -f ${PROGS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment