Makefile in The Top Directory

Working with Multiple Makefiles and Directories 127

4.4.4 Makefile in the tftp-dir Directory

The makefile in the tftp-dir directory builds the tftp target. It compiles and then statically links the object files using the library we built in the common-dir directory. This makefile is shown below. It also has rules to install the target and clean the directory. Variable definition SRCS = tftp.c OBJS = tftp.o HDRS = tftp.h CC = gcc CFLAGS = -g -O2 -c INCLUDES = -I..common-dir LIBSDIR = ..common-dir LDFLAGS = -static -LLIBSDIR INSTALLDIR = root Default Target tftp: SRCS HDRS CC CFLAGS INCLUDES tftp.c CC LDFLAGS COMMON OBJS -lcommon -o tftp install: cp tftp INSTALLDIR clean: echo Deleting files ... rm -f tftp OBJS ~

4.4.5 Makefile in the dns-dir Directory

The makefile in the dns-dir directory builds the dnsresolver target. It compiles and then statically links the object files using the library we built in the common-dir directory. This makefile is shown below. Variable definition SRCS = dnsresolver.c OBJS = dnsresolver.o HDRS = dnsresolver.h CC = gcc CFLAGS = -g -O2 -c INCLUDES = -I..common-dir LIBSDIR = ..common-dir LDFLAGS = -static -LLIBSDIR INSTALLDIR = root 128 Chapter 4 • Using GNU make Default Target dnsresolver: SRCS HDRS CC CFLAGS INCLUDES dnsresolver.c CC LDFLAGS COMMON OBJS -lcommon -o dnsresolver install: cp dnsresolver INSTALLDIR clean: echo Deleting files ... rm -f dnsresolver OBJS ~

4.4.6 Building Everything

After going through these makefiles, you are ready to build the targets. Go to the top directory and run the make command from there. It will read the makefile in the top directory and will try to build all targets. A typical output of this action is as follows: [rootconformix make] make BUILDING ALL TARGETS for i in common-dir ftp-dir tftp-dir dns-dir ; do \ cd i ; make ; \ done make[1]: Entering directory `rootmakecommon-dir gcc -g -O2 -c common.c ar -cr libcommon.a common.o ranlib libcommon.a make[1]: Leaving directory `rootmakecommon-dir make[1]: Entering directory `rootmakeftp-dir gcc -g -O2 -c -I..common-dir ftp.c gcc -static -L..common-dir ftp.o -lcommon -o ftp make[1]: Leaving directory `rootmakeftp-dir make[1]: Entering directory `rootmaketftp-dir gcc -g -O2 -c -I..common-dir tftp.c gcc -static -L..common-dir tftp.o -lcommon -o tftp make[1]: Leaving directory `rootmaketftp-dir make[1]: Entering directory `rootmakedns-dir gcc -g -O2 -c -I..common-dir dnsresolver.c gcc -static -L..common-dir dnsresolver.o -lcommon -o dnsresolver make[1]: Leaving directory `rootmakedns-dir [rootconformix make] Working with Multiple Makefiles and Directories 129 During the process of building all targets, you can see how make enters and leaves each directory and builds targets in each directory.

4.4.7 Cleaning Everything

The cleaning process is done the same way as we built all targets. Output of this process is shown below. Again you can see that make enters each directory, runs the make clean com- mand and then leaves the directory. The clean rule in makefiles present in each subdirectory is used to remove files. [rootconformix make] make clean rm -f ~ for i in common-dir ftp-dir tftp-dir dns-dir ; do \ cd i ; make clean ; \ done make[1]: Entering directory `rootmakecommon-dir rm -f common.o libcommon.a ~ make[1]: Leaving directory `rootmakecommon-dir make[1]: Entering directory `rootmakeftp-dir Deleting files ... rm -f ftp ftp.o ~ make[1]: Leaving directory `rootmakeftp-dir make[1]: Entering directory `rootmaketftp-dir Deleting files ... rm -f tftp tftp.o ~ make[1]: Leaving directory `rootmaketftp-dir make[1]: Entering directory `rootmakedns-dir Deleting files ... rm -f dnsresolver dnsresolver.o ~ make[1]: Leaving directory `rootmakedns-dir [rootconformix make] The make clean command finishes its operation after going through all subdirectories.

4.4.8 Making Individual Targets

Instead of using a single big makefile, you can also build individual targets using smaller makefiles. While building only one target, make will go into only that directory and build that target. The following output shows the output of the make command when you build only the ftp target. [rootconformix make] make ftp make[1]: Entering directory `rootmakeftp-dir gcc -g -O2 -c -I..common-dir ftp.c gcc -static -L..common-dir ftp.o -lcommon -o ftp make[1]: Leaving directory `rootmakeftp-dir [rootconformix make]