HMC Homepage      CS Home

How to use Make

The file group maintenance system

Make is a utility which allows a programmer to automate the recompilation of programs, among other things. With a properly constructed Makefile, the single command make can automatically recompile the program you are working on, so that you can quickly test your latest revision. For larger software projects a Makefile is essential to dividing up your program into separate compilation units so you do not have to rebuild the whole system with each modification. This document assumes you are working with a C++ program, but the make techniques apply to any language.

  1. In the directory with your source and header files, create a text file named Makefile or makefile.

  2. The Makefile will consist of a series of file dependency relationships, followed by the commands necessary to update the target file.

  3. A dependency relationship consists of the target file, such as an executable, followed by a colon and the files on which it depends, such as the source files. For example:

    calculate: calculate.cc calculate.h

    means that the file calculate depends on the two files calculate.cc and calculate.h.

  4. The commands needed to update the target file should follow the dependency line. While the dependency line should begin at the left margin, each line with command MUST begin with a TAB. For example:

    calculate: calculate.cc calculate.h
    g++ calculate.cc -o calculate
    means that the g++ command will be executed to compile calculate as shown whenever calculate.cc or calculate.h has been modified.

  5. The two lines shown in the above step can be a complete Makefile. Once it is written, you just need to execute make. Just type:

    % make

    as the command to compile your program.

  6. It is also possible to use make to help break your program into separate modules. You could write a program alphabetize.cc which uses functions from linkedlist.cc. Your Makefile might look like:

    alphabetize: alphabetize.cc alphabetize.h linkedlist.o
    g++ alphabetize.cc linkedlist.o -o alphabetize
    linkedlist.o: linkedlist.cc linkedlist.h
    g++ -c linkedlist.cc
    If you just make a change to alphabetize.cc or alphabetize.cc, only that first g++ line will be executed; linkedlist.cc will not be recompiled. If you just make a change to linkedlist.cc, however, it will be recompiled with the second g++ command, and then the first g++ command will be performed since linkedlist.o will be updated. Your Makefile automatically builds your program for you.

Notes:

This file has just explained the basic concepts behind make and the simplest way to use it. A Makefile of the complexity shown here can greatly ease the pain of recompiling your program after each change (like when debugging). The make program provides many more features useful for writing larger Makefiles to handle more complicated programming projects. Consult the man page make(1) for more information.


Copyright (c) HMC Computer Science Department. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License.''

HMC Computer Science Department
Contact Information
Last Modified Tuesday, 22-May-2001 15:19:47 PDT