Introduction to make

Introduction to the Unix make utility
Compiling a C program without make
Maintaining text files without make
How make can simplify your work
How to write a Makefile
Additional information
Miscellaneous tips
How make can simplify your work

In the previous two examples, I mentioned the command you might use to build a multiple-source-file C program, or to maintain a set of text files with some text in common.

Let's look at the C program. I mentioned that to rebuild the C program, you might issue the following commands:

cc -c sum.c
cc -c main.c
cc sum.o main.o -o my_sum

There are a few things one could improve on:

  1. It gets tiresome to type out all those long commands — particularly when you do so repeatedly.

  2. If someone else is compiling the program, they might not want to have to know the compile commands — they might prefer to just type a single command, and have the program know how to build itself. This would shield the user from the complexity of compiling a large program.

  3. If you've already built a program once, and then you only change one source file, there's no reason to recompile all the source files. Rather, you should only have to rebuild the ones that are affected by the changes you just made.

The make program addresses these needs.

You can put your source files (sum.h, main.c and sum.c), along with a special makefile (which we'll discuss in the next page) in a directory, then simply type make each time you make changes to any of your source files.

Anyone else who wants to build your program can just copy those four files, and type make — they don't have to know the (potentially gory) details of what it takes to build the program.

If you've already built your program; then you edit, say, sum.c; and then you type make, main.c won't be re-compiled — because it doesn't need to be. This is called build avoidance — rebuilding only what needs to be built. This becomes important for programs with hundreds or thousands of source files.