Explicit Rules The make Rules

122 Chapter 4 • Using GNU make Let’s try to build the default target for the first time and see the output of these echo commands. [rootconformix make] make gcc -g -O2 -c -o ftp.o ftp.c gcc -g -O2 -c -o common.o common.c ftp.o common.o ftp.h common.h ftp ftp.o gcc ftp.o common.o -o ftp [rootconformix make] The first two lines in the output use implicit rules to build object files. The third line in the output is generated by the first echo command and it has a list of all the dependencies. The sec- ond echo command displays the fourth line in the output, which is just the name of the target, i.e. ftp. The last echo command prints the first dependency in the fifth line of the output. Now let us modify the common.c file and run make once again. The output will be as follows: [rootconformix make] make gcc -g -O2 -c -o common.o common.c common.o ftp ftp.o gcc ftp.o common.o -o ftp [rootconformix make] This time make used an implicit rule to build common.o. The important thing to note is the second line in the output, which is displayed by the first echo command in the makefile. This time it displayed only one file in the dependency list because common.o is the only file that is changed in the dependencies. Automatic variables are very useful in control structures and in the decision-making pro- cess when you carry out an operation based upon the result of some comparison.

4.4 Working with Multiple Makefiles and Directories

Until now we have used simple makefiles to build projects or targets. We used only one makefile in the project and all of the source files were present in the same directory. In real-life software development projects, we have multiple directories containing different parts of a software prod- uct. For example, one directory may contain all library files, another header files and a third one common files used by different parts. Similarly every part of the software project may have its own subdirectory. This section deals with the same source files and targets that we used earlier in this chap- ter. The project contains three targets to be built. These targets are ftp, tftp and dnsre- solver. We are using the same set of source files. However to demonstrate the use of multiple directories and multiple makefiles, we have created each component in a separate directory. This