Variable Definitions

As the "makeit" program is simply a wrapper for the GNU make program, anything you can do in GNU make, you can also do with makeit. In relation to variable definitions, GNU make provides a number of different ways of setting makefile variables. The two main forms are illustrated by the example below.

VAR1 = VALUE1
VAR2 := $(VAR1)
VAR3 = $(VAR1)
VAR1 = VALUE2

When you use ":=" to set the value of a variable, as in "VAR2", the value on the right hand side of the assignment is evaluated immediately. That is, for the example above the variable "VAR2" will have the value "VALUE1" when later used. When you use "=" to set the value of a variable, as in "VAR3", the value on the right hand side of the assignment is not evaluated until the point at which the variable is used. This means that for the above example, the variable "VAR3" will have the value "VALUE2" and not "VALUE1" when used.

Because makeit greatly simplifies the makefiles you need to define, you will not typically have to rely on one behaviour or another. As a result, it doesn't matter which you use. You should still however be mindful of the difference between the two types of variable assignments. This is because of a problem which can arise when you wish to extend the value of an existing variable. To illustrate this, consider the following example.

VAR1 = VALUE1
VAR1 = $(VAR1) VALUE2

VAR2 := VALUE1
VAR2 := $(VAR2) VALUE2

For this case, if you were to use "=" and not ":=", when the variable "VAR1" was used, GNU make would complain about a loop, deriving from the fact that in evaluating the right hand side it would find a reference to the variable on the left hand side which if it was included and evaluated again would continue on ad infinitum. Use of ":=" does not suffer this problem as the right hand side will not contain variable references due to variables being expanded at the point the variable was defined and not when it is used.

If using ":=" to expand the value of a variable because the complete value will not fit on one line, you can instead use "+=" or the continuation character "\\". The continuation character simply allows the value to extend over more than one line. Use of "+=" will result in the variable being extended to include the extra value, it may even be used safely where the original variable was defined using "=".

It is also possible to use "+=" on a variable which had till that point not been assigned any value. In this case, the initial value of the variable will taken to be empty, with the variable being treated as if it was set using "=". Note that when you use "+=", a space will be automatically placed between the existing value and the value to be appended to the variable. If you need to extend a variable in such a way that you dont have a space between the values, you will need to use ":=".

VAR1 = VALUE1
VAR1 += VALUE2

VAR2 := VALUE1
VAR2 += VALUE2

VAR3 := \
  VALUE1 \
  VALUE2

VAR4 := VALUE1
VAR4 := $(VAR4):VALUE2

If you wish to know in more detail the mechanisms of variables or other features of GNU make, you should read the documentation covering GNU make. If you wish to use any of the more advanced features of makeit, it is advisable that you become more familiar with GNU make itself.


CategoryMakeit

Makeit/VariableDefinitions (last edited 2006-09-02 07:44:08 by GrahamDumpleton)