Working with Multiple Makefiles and Directories123
may not be the best example for a real-life situation, but it is well suited to demonstrate one of
many possible ways to handle such a situation. The following directory tree shows the arrange-
ment of these files in different directories.
[root@conformix make]# tree
.
|-- Makefile
|-- common-dir
| |-- Makefile
| |-- common.c
| `-- common.h
|-- dns-dir
| |-- Makefile
| |-- dnsresolver.c
| `-- dnsresolver.h
|-- ftp-dir
| |-- Makefile
| |-- ftp.c
| `-- ftp.h
`-- tftp-dir
|-- Makefile
|-- tftp.c
`-- tftp.h
4 directories, 13 files
[root@conformix make]#
As you can see, you have four directories, each having its own makefile as well as source
files. There is a makefile in the top directory that is used with the
make
command. This top-
level makefile used makefiles in sub-directories. We can carry out the common tasks of building,
installing and cleaning different targets while sitting in the top directory. Let’s have a look at
makefile in each directory.
4.4.1Makefile in The Top Directory
Makefile in the top directory is used to build all of the targets using different rules. To
build a particular target, we move to the directory where source files for that target are present
and run
make
in that directory. This makefile is shown below.
###################################################
# Makefile created to demonstrate use of the make
# utility in the "Linux Development Platform" book.
#
# Author: Rafeeq Ur Rehman
# rr@conformix.com
###################################################
# Variable definition