Tuesday, February 5, 2008

A Note on Makefile

GNU make is a utility to maintain groups of programs.

The make command allows you to manage large programs or groups of programs. As you begin to write larger programs, you will notice that re-compiling larger programs takes much longer than re-compiling short programs. Moreover, you notice that you usually only work on a small section of the program (such as a single function that you are debugging), and much of the rest of the program remains unchanged.

The make program aids you in developing your large programs by keeping track of which portions of the entire program have been changed, compiling only those parts of the program which have changed since the last compile.

Syntax:

make [ -f makefile ] [ option ] ... target ...

Usually, name of 'make' file is Makefile/makefile. But it can be any user-defined name.

@> make - will search for default makefile.

To make, makefile with user-defined name, command is
@> make -f myMake

Makefile works based on Rules.

Rules:

A rule tells Make both when and how to make a file.
As an example, suppose your project involves compiling source files main.c and add.c then linking them to produce the executable project.bin. Withholding a detailed explanation for a bit, here is a makefile using GNU C which will manage the task of making project.bin:

The Example Makefile

project.bin : main.obj add.obj
gcc -o project.bin main.obj add.obj
main.obj : main.c
gcc –c main.c
add.obj : add.c
gcc –c add.c

This makefile shows three rules, one each for making project.bin, main.obj, and add.obj. The rules as shown above are called explicit rules since they are supplied explicitly in the makefile. Make also has inference rules that generalize the make process.

Dependency Lines: When to Build a Target

The lines with the colon “:” in them are called dependency lines. They determine when the target is to be rebuilt.

To the left of the colon is the target of the dependency. To the right of the colon are the sources needed to make the target. A dependency line says “the target depends on the sources.” For example, the line:

project.bin : main.obj add.obj 

states that project.bin depends on main.obj and add.obj. At run time Make compares the time that project.bin was last changed to the times main.obj and add.obj were last changed. If either source is newer than project.bin, Make rebuilds project.bin. The last-changed time is the target's time as it appears in the file-system directory. This time is also known as the target's timestamp.

The Make Process is Recursive

It is a basic feature of Make that a target's sources are made before the timestamp comparison occurs. The line:

project.bin : main.obj add.obj 

implies “make main.obj and add.obj before comparing their timestamps with project.bin.” In turn:

main.obj : main.c 

says “make main.c before comparing its timestamp with main.obj.” You can see that if main.c is newer than main.obj, main.obj will be rebuilt. Now main.obj will be newer than project.bin, causing project.bin to be rebuilt.

For more info, $man make



@kova


No comments: