make

Contents

What is it good for

Make is a Unix program that helps you compile programs. It save times in the compilation process, because it only compiles the programs that need to be compiled. For example, when you compile programs using the command g+ main.cc encrypt.cc splash_screen.cc+ you compile splash_screen.cc each time even though you probably have not changed it since the first few weeks of the course. The make command looks at the creation times of the files and compiles files only when they have been changed since the last time they were compiled.

How to use it

You tell the make command how to compile your files by creating a Makefile. The Makefile tells make which files need to be compiled at each step. For example, the compile my encrypt program, I needed to compile main.cc, encrypt.cc, and splash_screen.cc. But, to compile encrypt.cc and splash_screen.cc, I did not need to compile any other programs. Therefore, I write a Makefile that tells make to check to see if splash_screen.cc and encrypt.cc have been changed whenever I need to compile main.cc. My makefile for encrypt looks like this:

all: main.cc encrypt.o splash_screen.o
          g++ main.cc encrypt.o splash_screen.o
  
  encrypt.o: encrypt.cc encrypt.h
          g++ -c encrypt.cc
  
  splash_screen.o: splash_screen.cc splash_screen.h
          g++ -c splash_screen.cc
  

Each pair of lines tells the compile how to make a different piece of the program. The word before the colon tells the name of the piece. The word on the first line after the color tells what make should check to see if it is up to date. The second line is the command that produces the piece before the colon.

Here, the first line says that to make all, the default because it is the first pair of lines, make needs to check to see that main.cc, encrypt.o and splash_screen.o have not changed since it was last compiled. To see if encrypt.o and splash_screen.o need to be compiled, it looks at the subsequent two pairs of lines. encrypt.o needs to be recompiled if encrypt.cc or encrypt.h have changed since it was last compiled; splash_screen.o needs to be remade if either splash_screen.cc or splash_screen.cc has changed. To make splash_screen.0, make executes the line g++ -c splash_screen.cc+. The \verb+-c+ flag tells \verbg++! just to compile the file and produce a splash_screen.o file rather than compiling and linking all of the files. The files are linked together in the top line when make executed the line g+ main.cc encrypt.o splash_screen.o+. The .o files are object files, files that have been compiled into machine language, but which have not been linked.

Checkers Example

My Makefile for the first checkers program is as follows:

FLAGS = -g
  
  all: main.cc board.o piece.o splash_screen.o
          g++ -o checkers ${FLAGS} main.cc board.o piece.o splash_screen.o
  
  clean:
          rm *.o checkers
  
  space:
          rm *~
  
  board.o: board.cc board.h
          g++ ${FLAGS} -c board.cc
  
  piece.o: piece.cc piece.h
          g++ ${FLAGS} -c piece.cc
  
  splash_screen.o: splash_screen.cc splash_screen.h
          g++ -c splash_screen.cc
  

Here, the first line sets the flags I am going to pass to the compiler every time I compile. The -g flag makes saves some information for gdb to work with. gdb is a debugging program we will look at in a later lesson. In addition, I use the -o flag to tell the compiler to put the compiled program in a file called checkers rather than in a.out.

The next two lines let me delete files in this directory. For example, when I use the command make clean, make will delete all of the .o files, and the file called checkers. When I use the command make space make will delete all of the files that end in ~ that emacs generated.

Caveat

The second line in a Makefile must start with a tab character. Unfortunately, the tab character looks just like a space and a tab, but a space and a tab will not work. Instead of compiling your file, make will produce the error message ``make: Fatal error in reader: Makefile, line 3: Unexpected end of line seen.'' To make sure that you have only a tab on the second line, move the cursor forward. It should jump immediately from the beginning of the line to the 'g' in g++.

More Information

make has a man page with lonts of information on it. It discusses both the make program and Makefiles.