Motivation cont. Problems Long files are harder to manage for both programmers and machines Every change requires long compilation Many programmers can not modify the same file
Trang 1Makefile Utility
Trang 2 Small program
Single file
“Not so small” programs
Many lines of code
Multiple components
More than one programmer
Trang 3Motivation (cont.)
Problems
Long files are harder to manage (for both programmers and
machines)
Every change requires long compilation
Many programmers can not modify the same file
simultaneously
Division to components is desired
Trang 4Program Compiling
Manual
Efforts
Shell script
Time
Makefile
Convenient
Compiling time
Trang 5 Text file
make
*NIX systems
Build, install, and uninstall programs
Avoid re-building targets which are up-to-date
Not limited to C/C++ programs
Trang 6Project:
main.c, sum.c, sum.h
sum.h is included in main.c & sum.c
executable file: sum
Trang 7Example (cont.)
Compile
$ gcc –o sum main.c sum.c
or
$ gcc –c main.c
$ gcc –c sum.c
$ gcc –o sum main.o sum.o
=> What happens if file main.c is modified
Trang 8Project Structure
sum.c sum.h main.c
sum.o main.o
sum
Trang 9Makefile content
# This is an example of Makefile
CC=gcc
sum: main.o sum.o
$(CC) –o sum main.o sum.o main.o: main.c sum.h
$(CC) –c main.c sum.o: sum.c sum.h
$(CC) –c sum.c Rule TAB
Target Dependency Action
Trang 10Built-in Macros
sum: main.o sum.o
gcc –o $@ $^
.c.o:
gcc –c $<
main.o sum.o: sum.h
gcc –c $*.c
Trang 11Built-in Macros (cont.)
$@ - The name of the target of the rule
$< - The name of the first dependency
$^ - The names of all the dependencies
$? - The names of all dependencies that are newer
than the target
$* - The name of the target of the rule without
extension
Trang 12Make Operation
Project dependencies tree is constructed
Target of first rule should be created
We go down the tree to see if there is a target that should
be recreated This is the case when the target file is older than one of its dependencies
In this case we recreate the target file according to the
action specified, on our way up the tree Consequently,
more files may need to be recreated
If something is changed, linking is usually necessary
Trang 13Make Operation (cont.)
File Last Modified
sum 10:03
main.o 09:56
main.c 10:45
Trang 14Make Operation (cont.)
sum.o main.o
sum
10:03
Trang 15Make Operation (cont.)
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)
Trang 16Questions???