Advertisement
NLinker

Makefile template done right

Feb 8th, 2021
2,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 1.24 KB | None | 0 0
  1. # from https://tech.davis-hansson.com/p/make/
  2. # use bash as the default shell
  3. SHELL := bash
  4. # ensures each Make recipe is ran as one single shell session, rather than one new shell per line
  5. .ONESHELL:
  6. # use bash strict mode. http://redsymbol.net/articles/unofficial-bash-strict-mode/
  7. .SHELLFLAGS := -eu -o pipefail -c
  8. # remove target files when Make file failed
  9. .DELETE_ON_ERROR:
  10. # warning when referring the undefined variables
  11. MAKEFLAGS += --warn-undefined-variables
  12. # disable the builtin rules. Only do what I tell you!
  13. MAKEFLAGS += --no-builtin-rules
  14.  
  15. # replace tabs
  16. ifeq ($(origin .RECIPEPREFIX), undefined)
  17.     $(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
  18. endif
  19. .RECIPEPREFIX = >
  20.  
  21. CC       := gcc
  22. CFLAGS   := -Wall -Werror
  23. ifeq ($(mode), debug)
  24. CFLAGS += -g
  25. endif
  26. OS       := $(shell uname -s)
  27. LIBS     :=
  28. ifeq ($(OS), Linux)
  29. LIBS += -pthread
  30. endif
  31.  
  32. SRCS     := all_src_files.c
  33. OBJS     := ${SRCS:c=o}
  34. PROGS    := ${SRCS:.c=}
  35.  
  36. .PHONY: all clean
  37. # Default - top level rule is what gets ran when you run just `make`
  38. all: ${PROGS}
  39.  
  40. ${PROGS} : % : %.o Makefile
  41. > ${CC} $< -o $@ ${LIBS}
  42.  
  43. %.o: %.c Makefile
  44. > ${CC} ${CFLAGS} -c $<
  45.  
  46. # Clean up
  47. clean:
  48. > rm -f ${OBJS}
  49. > rm -f ${PROGS}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement