CC      = gcc
CFLAGS  = -Wall -g
SOURCES = $(wildcard *.c)
HEADERS = $(wildcard *.h)
OBJS    = $(patsubst %.c,%.o,$(SOURCES))
BINARY  = program

# Notes:
# To compile multiple c-files into one binary, add them after each other as:
#   SOURCES = file1.c file2.c ...
# If you want all files in the current folder to be compiled together use:
#   SOURCES = $(wildcard *.c)
#
# In many projects you will be asked to use the following CFLAGS
#   CFLAGS  = -std=c99 -Wall -Wextra -pedantic -g
# It enforces the C99 standard as well as throwing all the warnings it can 
# (which can be annoying but it is good!)

# Making $(BINARY) phony enforces recompilation every time.
.PHONY: all run clean test

all: $(BINARY)

run: $(BINARY)
	./$(BINARY)

# Making any object file depend on all header files
# means that everything must be recompiled if a header file
# is updated. It is not that elegant but it is simple.
%.o: %.c $(HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

$(BINARY): $(OBJS)
	$(CC) $(OBJS) $(CFLAGS) -o $(BINARY)

clean:
	$(RM) $(OBJS) $(BINARY)
