A file called makefile tells the make utility in a structured manner which source and object files depend on other files. It also defines the commands required to compile and link the files.
Each file to build, or step to perform, is called a target. Each entry in a makefile is a rule expressing a target object's dependencies and the commands needed to build or make that object. The structure of a rule is:
target: dependencies-listTAB build-commands
Dependencies. Each entry starts with a line that names the target file, followed by all the files the target depends on.
Commands. Each entry has one or more subsequent lines that specify the Bourne shell commands that will build the target file for this entry. Each of these command lines must be indented by a tab character.
Suppose you have a program of four source files and the makefile:
makefile
commonblock
computepts.f
pattern.f
startupcore.f
Assume both pattern.f and computepts.f have an INCLUDE of commonblock, and you wish to compile each .f file and link the three relocatable files, along with a series of libraries, into a program called pattern.
pattern: pattern.o computepts.o startupcore.o f77 pattern.o computepts.o startupcore.o -lcore77 \ -lcore -lsunwindow -lpixrect -o pattern pattern.o: pattern.f commonblock f77 -c -u pattern.f computepts.o: computepts.f commonblock f77 -c -u computepts.f startupcore.o: startupcore.f f77 -c -u startupcore.f
The first line of this makefile indicates that making pattern depends on pattern.o, computepts.o, and startupcore.o. The next line and its continuations give the command for making pattern from the relocatable.o files and libraries.
Suppose you have a program of five source files and the makefile:
manythreads.cc
Makefilemany.cc
thr.cc
misc.h
defines.h
The target files are many, manythreads, and thrI.
all: many manythreads thrI many: many.cc CC -o many many.cc -g -D_REENTRANT -lm -lnsl -lsocket -lthread thrI: thr.cc CC -o thrI thr.cc -g -D_REENTRANT -lm -lnsl -lsocket -lthread manythreads: manythreads.cc CC -o manythreads -g -D_REENTRANT manythreads.cc -lnsl \ -lsocket -lthread
The first line of this makefile groups a set of targets with the label all. The succeeding lines give the commands for making the three targets, each of which has a dependency on one of the source files.