01.09.2014 Views

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CH04.fm Page 114 Monday, October 7, 2002 8:54 PM<br />

114 Chapter 4 • <strong>Using</strong> GNU make<br />

all: $(TARGETS)<br />

# Rule to build object files<br />

$(OBJS): $(SRCS)<br />

$(CC) $(CFLAGS) -c $(@:.o=.c)<br />

# Rules to build individual targets<br />

ftp: $(FTPOBJS) $(FTPHDRS)<br />

$(CC) $(LDFLAGS) $(FTPOBJS) -o ftp<br />

tftp: $(TFTPOBJS) $(TFTPHDRS)<br />

$(CC) $(LDFLAGS) $(TFTPOBJS) -o tftp<br />

dnsresolver: $(DNSRESOLVEROBJS) $(DNSRESOLVERHDRS)<br />

$(CC) $(LDFLAGS) $(DNSRESOLVEROBJS) -o dnsresolver<br />

clean:<br />

rm -f $(TARGETS) $(OBJS)<br />

install:<br />

cp $(TARGETS) $(INSTALLDIR)<br />

# Additional Dependencies<br />

ftp.o: $(FTPHDRS) tftp.o: $(TFTPHDRS)<br />

dnsresolver.o: $(DNSRESOLVERHDRS)<br />

After comments in the start of the makefile, the first part of the file defines variables used<br />

in this file. As you shall see, defining variables is very helpful if you need any modification in<br />

the Makefile or want to add or remove more files later on. After defining variables, the default<br />

target is defined with the name "all" as follows:<br />

all: $(TARGETS)<br />

This in turn has dependencies on the three targets that we want to make. Please note that<br />

we don't have any comm<strong>and</strong> to build this default target. When make reaches the default target, it<br />

tries to meet dependencies to make sure that these are up-to-date. If any of these dependencies is<br />

not up-to-date, make will try to recreate it.<br />

<strong>The</strong> three dependencies to the default target (ftp, tftp <strong>and</strong> dnsresolver) have their<br />

own rules to build. For example, the following ftp rule shows that it depends upon two variables<br />

$(FTPOBJS) <strong>and</strong> $(FTPHDRS).<br />

ftp: $(FTPOBJS) $(FTPHDRS)<br />

$(CC) $(LDFLAGS) $(FTPOBJS) -o ftp<br />

This means that if any of the files listed in these two variables has changed, the ftp target<br />

will be rebuilt. <strong>The</strong>se files defined in the two variables are as follows:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!