Using Makefile

Using Makefile


What is a Makefile?

A Makefile is a special text file used to create a sequence of commands for execution by the UNIX shell. A common usage is to sort out dependency relations among files. For example, in LaTeX a postscript file is created from a .dvi file which in turn is created from a .tex file.

How to write a simple Makefile

Create a file and call it Makefile or makefile. It is not necessary to make this file executable. Add the following lines:

# Makefile to create a postscript file
# from a latex file 

report.ps : report.dvi
	dvips report.dvi -o report.ps 

report.dvi : report.tex
	- latex report.tex

The name to the left of the colon is called the target. The name(s) to the right of the colon are called dependencies. One or more command lines follow, each beginning with a tab.

The target report.ps depends on report.dvi and the command to make report.ps is dvips report.dvi -o report.ps. Similarly with the target report.dvi which depends on report.tex. To initiate this sequence of events simply type make. Assuming there exists a LaTeX file in the directory called report.tex, the following commands will be executed (the "-" before latex tells the Makefile to ignore any errors that may occur):

	- latex report.tex
	dvips report.dvi -o report.ps

Because report.ps depends on report.tex, once report.ps is created, subsequent calls to make will report: `report.ps' is up to date. The postscript file report.ps will only be created if report.tex has been edited/changed since report.ps was last created.

This Makefile can be made more general by using macros:

FILE = report

$(FILE).ps : $(FILE).dvi 
	dvips $? -o $@ 

$(FILE).dvi : $(FILE).tex
	- latex $? 

Where $(FILE) evaluates to the value of the variable FILE, $? evaluates to the current dependency and $@ evaluates to the current target. This Makefile can then be used for any LaTeX file by simply changing one line.

Odds and Ends