| 1 | # Basic Makefile for a C program with multiple source files and header dependencies |
| 2 | |
| 3 | CC = gcc |
| 4 | CDFLAGS = -Wall -Wextra -std=c17 -I include |
| 5 | CFLAGS = $(CDFLAGS) -g -O0 -MMD -MP |
| 6 | BUILD = build |
| 7 | EXAMPLES_SRC = $(wildcard examples/*.c) |
| 8 | EXAMPLES_OBJ = $(patsubst %.c, $(BUILD)/%.o, $(EXAMPLES_SRC)) |
| 9 | EXAMPLES = $(patsubst examples/%.c, %, $(wildcard examples/*.c)) |
| 10 | LIB_SRC = $(wildcard coroutine/*.c) |
| 11 | LIB_OBJ = $(patsubst %.c, $(BUILD)/%.o, $(LIB_SRC)) |
| 12 | OBJ = $(EXAMPLES_OBJ) $(LIB_OBJ) |
| 13 | DEP = $(OBJ:.o=.d) |
| 14 | |
| 15 | # Have $(OBJ) as a dependenacy for $(ALL) to avoid them being treated as intermediates which are deleted after build |
| 16 | all: $(EXAMPLES) $(OBJ) |
| 17 | |
| 18 | build: |
| 19 | mkdir $(BUILD) |
| 20 | |
| 21 | %: $(BUILD)/examples/%.o $(LIB_OBJ) |
| 22 | $(CC) $(CFLAGS) -o $@ $^ |
| 23 | |
| 24 | $(BUILD)/%.o: %.c build |
| 25 | $(CC) $(CFLAGS) -c $< -o $@ -MF $(@:.o=.d) |
| 26 | |
| 27 | -include $(DEP) |
| 28 | |
| 29 | clean: |
| 30 | echo Doing clean |
| 31 | rm -f $(OBJ) $(DEP) $(EXAMPLES) |
| 32 | |
| 33 | .PHONY: all clean |
| 34 | |