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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

<strong>Using</strong> Variables 119<br />

You can modify this file so that it uses implicit rules to build object files. <strong>The</strong> modified file<br />

is shown below:<br />

# Variable definition<br />

OBJS = ftp.o common.o<br />

HDRS = ftp.h common.h<br />

CFLAGS = -g -O2<br />

TARGETS = ftp<br />

CC = gcc<br />

# Default Target<br />

ftp: $(OBJS) $(HDRS)<br />

$(CC) $(OBJS) -o ftp<br />

clean:<br />

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

Note that we have completely taken out two rules that are used to build the object files.<br />

Now when make needs to build these object files, it uses its implicit rule for this purpose. Running<br />

make on this makefile produces the following result.<br />

[root@conformix make]# make<br />

gcc -g -O2 -c -o ftp.o ftp.c<br />

gcc -g -O2 -c -o common.o common.c<br />

gcc ftp.o common.o -o ftp<br />

[root@conformix make]#<br />

Note that the first two lines of the output create object files using implicit rules. You may<br />

also have noted that the CFLAGS variable is also used in this process. Like CFLAGS, implicit<br />

rules use other variables while building targets. For a more detailed discussion, please see the<br />

reference at the end of this chapter.<br />

While using implicit rules, you should be careful about the process, because make can<br />

build a target using explicit rule depending upon which source files are available. For example,<br />

make can produce an object file from a C source code file as well as Pascal source code file. In<br />

the above example, if common.c file is not present but common.p (Pascal source code file) is<br />

present in the current directory, make will invoke Pascal compiler to create common.o file,<br />

which may be wrong. You also have less control over options on implicit rules.<br />

4.3 <strong>Using</strong> Variables<br />

Variables are an important part of all makefiles used in a project. Variables are used for many<br />

purposes; the most important of which is to have structured <strong>and</strong> easily underst<strong>and</strong>able makefiles.<br />

This section contains more information about variables used in makefiles.

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

Saved successfully!

Ooh no, something went wrong!