# See https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
# for implicit variables and their default values

# See https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html
# for a list of built-in rules
# or use make -p (in a folder wihtout makefile)

# ------ IMPLICIT VARIABLES --------

# C compiler
# defaults to cc (cc = symbolic link that points to the system's default C compiler)
# we use gcc explicitely
CC = gcc

# extra flags to give to the C compiler
# defaults to the empty string
# -Wall -Wextra - show warning message
# -O2 optimization level
CFLAGS = -Wall -Wextra -O2

# --------- USER-DEFINED VARIABLES --------
# final executable
EXECUTABLE = hello
# source files
SOURCES = hello.c
# object files - source files where extension .c is replaced by .o
OBJECTS = $(SOURCES:.c=.o)

# ------ RULES ------------

# phony targets = targets not associated with a file
# if the target is explicitly marked as phony, conflicts with a file of the same name are avoided
.PHONY : all clean

# default target
all: $(EXECUTABLE)

# compile sources
# $< references prerequisite (dependency)
# $@ references target
# (this is an implicit rule and can be ommitted)
# %.o: %.c
#	$(CC) $(CFLAGS) -c $< -o $@  

# link
$(EXECUTABLE): $(OBJECTS)
	$(CC) $(OBJECTS) -o $(EXECUTABLE)

clean:
	rm -f $(EXECUTABLE) $(OBJECTS)