Unix Makefile QuickStart

The "make" utility uses a "makefile" that contains information on how a program should be compiled and linked; if any source files have been modified since the last "make" of the program, the make utility will figure out exactly what needs to be recompiled and relinked, and do it. So it "makes" a new version of the executable file, doing only what work is needed - it updates the executable automatically.

This is helpful even for simple projects, to avoid having to retype the compile command every time. But it is an essential tool for complex projects like those later in the course, so we recommend that you get started using it now.

The following is how to use "make" for a simple project.



  1. Using your text editor, create a "makefile" file named for example "p1make". Put the following lines in it:


    p1app: p1.cpp TABg++ -o p1app p1.cpp

    "p1app" is the name of the executable file (you can use your own name).
    p1.cpp is the name of the source code file (you can use your own name).

    The first line is a "dependency line" - it says that the file p1app depends on the file p1.cpp. If p1.cpp has been modified since the last make, then the next line in the makefile will be executed to get the updated version of p1app. The text on the first line must start in column one of the line - no white space at the beginning!

    The TAB in the second line represents a TAB character. Don't type in "TAB", hit the "tab" key on the keyboard. A TAB character must be present at the beginning of this line.

    The rest of the second line is just the regular compile command that you would normally issue. This is a "command line".

  2. Quit the text editor.

  3. Anytime you want to recompile your project, issue the make command with the name of your "makefile":

    make p1make

    The make utility will apply the dependency rule in the makefile, and recompile the program only if necessary. Suppose you start a work session: you remember changing the source file, but you can't remember whether you recompiled the new changes or not. Just issue the make command, and it will recompile only if needed. Make will save a lot of time for a complex project because only modified source code files will be recompiled.

    If you give your makefile the name "makefile", then the "make" command will automatically use it, resulting in even more convenience. Just enter the make command by itself:

    make

See Winston Ch 46 for how to use make for projects with multiple source files, which we will be doing later in the course.