Contents|Index|Previous|Next
Variables make makefiles simpler

In the first example shown in A simple makefile, we had to list all the object files twice in the rule for ‘edit’ which isrepeated in this next example.
 

Such duplication is error-prone; if a new object file is added to the system, we might add it to one list and forget the other. We can eliminate the risk and simplify the makefile by using a variable. Variables allow a text string to be defined once and substituted in multiple places later (see How to use variables).

It is standard practice for every makefile to have a variable named objects, OBJECTS, objs, OBJS, obj,or OBJ which is a list of all object file names. We would define such a variable, objects, with input like the following in the makefile.
 

Then, each place we want to put a list of the object file names, we can substitute the variable’s value by writing ‘$(objects)’ ( for more information, see How to use variables). The following example shows how the complete simple makefile looks when you use a variable for the object files.
 


Top|Contents|Index|Previous|Next