CMSC104 CSEE | UMBC

make system

The make system is a development tool that will allow you to quickly and efficiently recompile a program. What makes it quick is that all you have to recompile is the files that changed, plus any files that depend on another file that changed. The make system requires a Makefile. Makefile example
gary : gary.o func1.o 
        cc -o gary gary.o func1.o 
gary.o : gary.c gary.h 
        cc -c gary.c 
func1.o : func1.c gary.h 
        cc -c func1.c 
clean : 
        rm gary gary.o func1.o 

Example explained

Suppose we have a program whose source code is in two C source code files (gary.c and func1.c) and has one header file (gary.h) that both C source files need to compile correctly, and therefore are dependent on the header file being the most up-to-date version when each of the source code files are compiled, assuming they can be compiled independently. If gary.c is modified, then only that file must be recompiled and the program relinked. If only func1.c is modified, then too, only one file must be recompiled and the program relinked. However, if gary.h is modified, then both gary.c and func1.c must be recompiled and everything relinked.

It would be nice if there was a way to rebuild the program gary with the minimum of recompiling! The make system will let us do that by specifying the rules for what depends on what.

This file consists of four rules. Each rule has two lines.

The first line consists of the target name, a colon, and the files to be checked. The second line starts with a tab character, and the action to be taken. The first three rules all start the cc compiler, while the fourth rule removes the old object files and the executable file. Notice in the second and third rule, there is a option of -c which tells the compiler to only compile the source code to object code.

The first rule invokes the compiler differently. The -o option give the name that the compile will use when creating the executable file. The arguments are the object files used to create the executable.

The command to use this system is make and the target.

make gary In this case, it will look at the file gary to see if gary.o or func1.o are newer than gary or if gary does not exist. In that case it will create the new executable. To make sure that it is getting the lastest version of everything, before it uses gary.o, it looks at the rule for gary.o to make sure it is up-to-date. If not, it will do the action specified, compile gary.c. It must also do the same thing for func1.c before attempting the relink. This will then result in the least possible number of recompiles, meaning the program is build in the fastest way. Additionally, this is a reliable way to make sure that everything necessary actually does get recompiled.



UMBC CMSC421 CSEE | UMBC