Back to Blog

Makefile Example Explained (Fully Annotated)

#Makefile#Wildcard

# For 2416 Project
# Written by lx-gzjp-2011-7-19

#*********** setting **********************************************
CC := gcc
OUTDIR := obj
OUTBINNAME := go
SRC_DIR := ./ 2416def/ bmp/ lcddriver/ disp/ zklib/ gps/ usb/ timer/ anet/ omc/
#*****************************************************************

LINK := $(CC) -o
LINETHREAD := -lpthread -lm
CFLAG := -c -O2 -o
SRC_SUFFIX := .c
OBJSUFFIX := .o
SRCS := $(foreach n, $(SRC_DIR), $(wildcard $(n)*$(SRC_SUFFIX)))
OBJECTS := $(patsubst $(SRC_SUFFIX), $(OUTDIR)/$(OBJ_SUFFIX), $(notdir $(SRCS)))

all : $(OUTDIR) $(OUTDIR)/$(OUTBINNAME)

.PHONY : all

$(OUTDIR):
mkdir $(OUTDIR)
@

$(OUTDIR)/$(OUTBINNAME): $(OBJECTS)
$(LINK) $@ $(OBJECTS) $(LINETHREAD)
@echo "Finished!"
@echo "Bin file is $(OUTDIR)/$(OUTBINNAME)!"

$(OUTDIR)/main.o: main.c
$(CC) $(CFLAG) $<

$(OUTDIR)/%.o: %.c
$(CC) $(CFLAG) $< -o $@

clean:
@rm -rf obj
@echo "$(OUTDIR) has been deleted!"

======================= Understanding Makefile =========================================================

edit (final target) : main.o kbd.o command.o display.o insert.o search.o files.o utils.o (intermediate object files are dependencies of final target)
cc -o edit main.o kbd.o command.o display.o insert.o search.o files.o utils.o // generate final executable from multiple .o files
main.o : main.c defs.h
cc -c main.c // no explicit output? This follows implicit rules where .o is directly derived from the source

================================================================================================