Programming in C

(Barry) #1

344 Chapter 15 Working with Larger Programs


says that each object file depends on its corresponding source file. So, if a source file
changes, its corresponding object file must be rebuilt.The makeutility has built-in rules
that tell it how to do that.
Here’s what happens the first time you run make:
$ make
gcc -c -o mod1.o mod1.c
gcc -c -o mod2.o mod2.c
gcc -c -o main.o main.c
gcc mod1.o mod2.o main.o -o dbtest
$
That’s kind of nice! makecompiled each individual source file and then linked the result-
ing object files to create the executable.
If you instead had an error in mod2.c, here’s what the output from makewould have
looked like:
$ make
gcc -c -o mod1.o mod1.c
gcc -c -o mod2.o mod2.c
mod2.c: In function 'foo2':
mod2.c:3: error: 'i' undeclared (first use in this function)
mod2.c:3: error: (Each undeclared identifier is reported only once
mod2.c:3: error: for each function it appears in.)
make: *** [mod2.o] Error 1
$
Here,makefound there was an error in compiling mod2.cand stopped the makeprocess,
which is its default action.
If you correct mod2.cand run makeagain, here’s what happens:
$ make
gcc -c -o mod2.o mod2.c
gcc -c -o main.o main.c
gcc mod1.o mod2.o main.o -o dbtest
$
Notice that makedidn’t recompile mod1.c.That’s because it knew it didn’t have to.
Therein lies the real power and elegance of the makeutility.
Even with this simple example, you can use the sample Makefileto start using make
for your own programs. Appendix E, “Resources,” tells you where you can turn for more
information on this powerful utility.

The cvsUtility


This is one of several utilities for managing source code. It provides for automatic ver-
sion-tracking of source code, and keeps track of changes that are made to a module.This
allows you to re-create a particular version of a program if needed (either to roll back
Free download pdf