Online make manual.

We are going to create an executable named myprogram.exe using 3 source files: driver.c, bytes.c, and scantext.c and this is the dependency diagram:

             


A very basic makefile:

myprogram.exe : driver.o scantext.o bytes.o
	gcc driver.o scantext.o bytes.o -o myprogram.exe

driver.o : driver.c scantext.h bytes.h
	gcc -Wall -Wextra -ansi -pedantic -O -c driver.c -o driver.o

scantext.o : scantext.c
	gcc -Wall -Wextra -ansi -pedantic -O -c scantext.c -o scantext.o

bytes.o : bytes.c bytes.h
	gcc -Wall -Wextra -ansi -pedantic -O -c bytes.c -o bytes.o

clean : 
	rm driver.o scantext.o bytes.o myprogram.exe

rebuild : 
	-$(MAKE) clean
	-$(MAKE) 
Using macros (variables):
# Macros #######################################################################
CC=gcc
CFLAGS=-Wall -Wextra -ansi -pedantic -O
OUTDIR=build/
OBJECTS=$(OUTDIR)driver.o $(OUTDIR)scantext.o $(OUTDIR)bytes.o
EXE=$(OUTDIR)program.exe
ERASE=rm

# Targets ######################################################################
$(EXE) : $(OBJECTS)
	$(CC) -o $(EXE) $(OBJECTS)

$(OUTDIR)driver.o : driver.c scantext.h bytes.h
	$(CC) -c driver.c -o $(OUTDIR)driver.o $(CFLAGS)

$(OUTDIR)scantext.o : scantext.c
	$(CC) -c scantext.c -o $(OUTDIR)scantext.o $(CFLAGS)

$(OUTDIR)bytes.o : bytes.c bytes.h
	$(CC) -c bytes.c -o $(OUTDIR)bytes.o $(CFLAGS)

clean : 
	$(ERASE) $(OBJECTS) $(EXE)

rebuild : 
	-$(MAKE) clean
	-$(MAKE) 
If you name your file makefile, then you don't have to tell make which file to use. This is the recommended way of doing it.