## An Introduction to Make files ## Joel Sutton Make could possibly be classified as one of the most useful, and fundamental, utilities available to the UNIX programmer. It controls the compilation, linking and often the installation of a large number of Open Source software packages. A fine example of an application of the make utility is the FreeBSD ports collection. Although I will not attempt analyse the complexity of that particular system, I will outline just how easy it is to construct a simple Makefile to compile your favorite piece of code. Here is an example of a simple Makefile that compiles and installs a simple program called zap.c: # Make file for the zap software package # by Joel Sutton # Version: 0.1 all: zap zap: gcc -o zap zap.c install: all install -s -o bin -g bin zap /usr/local/bin/zap clean: rm *.o zap # # EOF If you look at this example carefully you can probably figure out most of the syntax. You'll notice that there are three lines which start with a word followed by a colon. These lines are called targets. To the right of the colon we have the dependencies for that particular target. Make will check these dependent targets to ensure that they have been completed before processing the nominated target. The text which is indented, and directly beneath each target label, contains the commands that make must execute whilst completing the target. They can be any valid command that you can run from the shell but you would usually pick commands which will compile or install your program. The target name usually, but not always, corresponds to the file you wish to generate. In this case we want to make a binary called zap - hence the target of that name. If we needed to create another object file we would need another target: # Make file for the zap software package # by Joel Sutton # Version: 0.2 all: zap zap: screen.o zap.o gcc -o zap zap.o screen.o -lcurses screen.o: gcc -c -o screen.o screen.c zap.o: gcc -c -o zap.o zap.c install: all install -s -o bin -g bin zap /usr/local/bin/zap clean: rm *.o zap # # EOF A dependency in the zap target, and a new zap.o target, is required to ensure that screen.o is compiled before make tries to link it into the final zap binary. Matching your target names with actual file names allows Make to keep track of which files have been compiled and which haven't. This is especially useful in a large project because make will not rebuild the entire package if you edit one single file (this is important if you're using a 486DX). Whenever you run make you can supply the name of the target you wish to start with. If you don't supply a name then the default tag is the first one it finds, in this case all. After everything has been compiled successfully. We're ready to install the package with make install. Hopefully you'll be starting to get the hang of the Makefile format and you'll be able to see that the install command copies the binary into /usr/local/bin. Download these examples and experiment with them in your own time. Once you've outgrown this simple frame work be sure to read make(1), install(1), and the documentation for gcc. If you're looking for examples check out the source code from your favorite ports collection application. - Joel $Id: makefiles.txt,v 1.1 2000/02/16 08:07:51 jim Exp $