| # Basic Makefile for a C program with multiple source files and header dependencies |
|
| CC = gcc |
| CDFLAGS = -Wall -Wextra -std=c17 -I include |
| CFLAGS = $(CDFLAGS) -g -O0 -MMD -MP |
| BUILD = build |
| EXAMPLES_SRC = $(wildcard examples/*.c) |
| EXAMPLES_OBJ = $(patsubst %.c, $(BUILD)/%.o, $(EXAMPLES_SRC)) |
| EXAMPLES = $(patsubst examples/%.c, %, $(wildcard examples/*.c)) |
| LIB_SRC = $(wildcard coroutine/*.c) |
| LIB_OBJ = $(patsubst %.c, $(BUILD)/%.o, $(LIB_SRC)) |
| OBJ = $(EXAMPLES_OBJ) $(LIB_OBJ) |
| DEP = $(OBJ:.o=.d) |
|
| # Have $(OBJ) as a dependenacy for $(ALL) to avoid them being treated as intermediates which are deleted after build |
| all: $(EXAMPLES) $(OBJ) |
|
| build: |
| mkdir $(BUILD) |
|
| %: $(BUILD)/examples/%.o $(LIB_OBJ) |
| $(CC) $(CFLAGS) -o $@ $^ |
|
| $(BUILD)/%.o: %.c build |
| $(CC) $(CFLAGS) -c $< -o $@ -MF $(@:.o=.d) |
|
| -include $(DEP) |
|
| clean: |
| echo Doing clean |
| rm -f $(OBJ) $(DEP) $(EXAMPLES) |
|
| .PHONY: all clean |
|