C Makefile

# Makefile
# Project's name (Executable will be named after this variable)
TARGET = projectname
# Linker and its flags
LINKER = gcc
LFLAGS = -Wall -I. -lm
# Compiler and its flags
CC = gcc
CFLAGS = -std=c99 -Wall -I -lm
# Project's directories
SRCDIR = ./src
OBJDIR = ./build
BINDIR = ./bin
SOURCES := $(wildcard $(SRCDIR)/*.c)
OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
MAIN = $(BINDIR)/$(TARGET)
all: directories $(MAIN)
$(MAIN): $(OBJECTS) 
$(LINKER) $(LFLAGS) $(OBJECTS) -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(SRCDIR)/%.h
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.c 
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: directories
directories: $(OBJDIR) $(BINDIR)
$(OBJDIR):
mkdir -p $(OBJDIR)
$(BINDIR):
mkdir -p $(BINDIR)
clean: 
rm -rf $(OBJDIR)
veryclean: clean
rm -rf $(BINDIR)