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