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:
Using macros (variables):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)
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.# 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)
make
make -f make-gcc
This would execute the clean target only.make clean
This would use the clang compiler instead of the gcc compiler.make CC=clang
This will build the program, myprogram.exe, then run it and redirect the output to myoutput.txt, then diff it with the master output, output.txt, then run tablen on scantext.c and then run tablen on bytes.c.myprogram.exe : driver.o scantext.o bytes.o gcc driver.o scantext.o bytes.o -o myprogram.exe ./myprogram > myoutput.txt diff myoutput.txt output.txt tablen scantext.c tablen bytes.c
You may want to make a separate rule instead for everthing else like this:
and then you would execute it by running make like this:run : ./myprogram > myoutput.txt diff myoutput.txt output.txt tablen scantext.c tablen bytes.c
make run