Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Last active July 28, 2020 04:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pagliacii/c8c48c3f63dd48aece474c636a3111a3 to your computer and use it in GitHub Desktop.
Save Pagliacii/c8c48c3f63dd48aece474c636a3111a3 to your computer and use it in GitHub Desktop.
Makefile template, usage: `make all_src_files[ debug=mode]`. From https://tech.davis-hansson.com/p/make/
# 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
.ONESHELL:
# use bash strict mode. http://redsymbol.net/articles/unofficial-bash-strict-mode/
.SHELLFLAGS := -eu -o pipefail -c
# remove target files when Make file failed
.DELETE_ON_ERROR:
# 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)
endif
.RECIPEPREFIX = >
CC := gcc
CFLAGS := -Wall -Werror
ifeq ($(mode), debug)
CFLAGS += -g
endif
OS := $(shell uname -s)
LIBS :=
ifeq ($(OS), Linux)
LIBS += -pthread
endif
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
clean:
> rm -f ${OBJS}
> rm -f ${PROGS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment