Occasionally you should take a snapshot of the sources and the object files that they produce. Building an entire project involves invoking make successively in each subdirectory to build and install each module. The following example shows how to use nested make commands to build a simple project.
Assume your project is located in two different subdirectories, bin and lib, and that in both subdirectories you want make to debug, test, and install the project.
First, in the projects main, or root, directory, you put a makefile such as this:
# Root makefile for a project.
TARGETS= debug test install
SUBDIRS= bin lib
all: $(TARGETS)
$(TARGETS):
@for i in $(SUBDIRS) ; \
do \
cd $$i ; \
echo "Current directory: $$i" ;\
$(MAKE) $@ ; \
cd .. ; \
done
Then, in each subdirectory (in this case, bin) you place a makefile of this general form:
#Sample makefile in subdirectory
debug:
@echo " Building debug target"
@echo
test:
@echo " Building test target"
@echo
install:
@echo " Building install target"
@echo
When you type make (in the base directory), you get the following output:
$ make
Current directory: bin
Building debugging target
Current directory: lib
Building debugging target
Current directory: bin
Building testing target
Current directory: lib
Building testing target
Current directory: bin
Building install target
Current directory: lib
Building install target
$