Working with Multiple Makefiles and Directories125
After defining variables, we have a rule for each target. This rule basically has two com-
mands on the same line. The first command,
cd,
is used to change the directory where source
files for that target are located. In the second command,
make
uses makefile in that directory to
build that target. Please note that we can also use the
$(MAKE)
variable for this purpose.
In the
clean
and
install
rules, we use the for loop to go into each directory and exe-
cute some commands. The
for
loop is explained later in this chapter.
4.4.2Makefile in common-dir Directory
The files in the
common-dir
directory are used to build a simple library that is used by
other targets at link time. The makefile in this directory is listed below:
# Variable definition
SRCS = common.c
OBJS = common.o
HDRS = common.h
LIBCOMMON = libcommon.a
INSTALLDIR = /root
CC = gcc
CFLAGS = -g -O2 -c
# Default Target
$(LIBCOMMON): $(SRCS) $(HDRS)
$(CC) $(CFLAGS) common.c
ar -cr $(LIBCOMMON) $(OBJS)
ranlib $(LIBCOMMON)
N O T E Please note that the following lines:
ftp:
@cd $(FTPDIR) ; make
are not equal to the following three lines:
ftp:
@cd $(FTPDIR)
make
In the first case,
make
changes the directory to
$(FTPDIR)
and then executes the
make
command in that directory, which is
the right thing to do. However in the second case, the cd com-
mand is executed and after that the next
make
command is again
executed in the current directory. The effect of the cd command is
lost when
make
goes to the next line to execute the next com-
mand. This is because of a new instance of sub-shell. that exe-
cutes commands in each line as discussed earlier in this chapter.
Next Page >>
<< Previous Page
Back to the Table of Contents