Unix Makefiles
Unix Makefiles
Introduction
• The “make” utility in Unix is one of the original tools designed
by S. I. Fieldman of AT&T Bell labs circa 1977. There are many
version.
main.o sum.o
• Operations performed:
gcc –c main.c
gcc –o sum main.o sum.o
• main.o should be recompiled (main.c is
newer).
• Consequently, main.o is newer than sum
and therefore sum should be recreated (by
re-linking).
Components of a Makefile
• Comments
• Rules
• Dependency Lines
• Shell Lines
• Macros
• Inference Rules
Comments
• A comment is indicated by the character “#”. All text that
appears after it will be ignored by the make utility until the
end of line is detected.
• Example
– #
– # This is a comment
Rules
• Rules tell make when and how to make a file. The format is as
follows:
– A rule must have a dependency line and may have an action or shell
line after it. The action line is executed if the dependency line is out
of date.
– Example:
hello.o: hello.cpp
g++ -c hello.cpp
tab
dependency action
makefile
• At the running of the make utility, the time and date when
sum.exe was last built are compared to the dates when
main.obj and sum.obj were built.
• A target can have more than one shell line. Each line
must be preceded by a tab.
• The first shell line that returns an exit status of non-zero will
cause the make utility to stop and display an error.
Macros
• Comes from the Greek word makros meaning large.
• Examples:
• %.obj : %.c
• $(CC) $(FLAGS) –c $(.SOURCE)