Suppose you're writing some huge program with a bazillion files and headers. Wouldn't it be nice to somehow be able to compile the entire thing with a single incantation? (as opposed to typing "gcc -c fooX.c ..." for X in [0, 100])
Suppose you just edited one tiny header file. Wouldn't it be nice to be able to recompile only the files that are affected by the change?
Makefiles allow you to do precisely this. With only minimal knowledge of makefiles you can automagically recompile your program with the "make" command. Here's a quick and dirty intro :
CC = gccmeans that the variable CC contains "gcc". You access this variable by doing a $(CC) wherever you need it.
myprogram: fileA.o fileB.o fileC.o
$(CC) -o executablename fileA.o fileB.o fileC.o -lm
This is a dependency and it means that the target "myprogram" should invoke the compiler whenever fileA.o, fileB.o, or fileC.o change. In particular, it invokes the compiler referred to in the variable "CC" and creates an executable called "executablename".
Here's a segment of the template Makefile from project 1.
# the name of the target program TARGET = mytest # other source files and the associated object files (this can be blank) SRC = file1.c file2.c OBJ = $(SRC:.c=.o) # special include directories INCLUDE = -I. # special libraries (none for now) LIB = # select the compiler and flags CC = /usr/local/bin/gcc CFLAGS = -ansi -g .SUFFIXES: .c # specify how to compile the .c files .c.o: $(CC) $(CFLAGS) $(INCLUDE) -c $< # if you type 'make' without arguments, this is the default all: $(TARGET) # specify how to compile the target $(TARGET): $(OBJ) $(TARGET).c $(CC) $(CFLAGS) $(INCLUDE) $(TARGET).c $(OBJ) $(LIB) -o $(TARGET) # remove binaries clean: rm -f $(OBJ) $(TARGET).o $(TARGET) # remove binaries and other junk clobber: make clean rm -f core *~ # this will add the file dependencies below, i.e. it modifies this file depend: makedepend -- $(CFLAGS) -- $(INCLUDE) -- $(SRC) $(TARGET).c # DO NOT DELETE THIS LINE -- make depend depends on it. file1.o: file1.c file1.h /usr/include/stdio.h file2.o: file2.c file2.h file1.h /usr/include/stdio.h
/usr/local/bin/gcc -ansi -g -I. mytest.c file1.o file2.o -o mytest