Complex Variable names and Macro Expansions

Rarely is it necessary to apply what is discussed in this topic, but the information will be given for completeness. Because of the recursive nature of the macro expansion algorithm, it is possible to construct variables that act like arrays (or other complex data types) through the use of variable names that themselves contain a nested macro expansion. Here is an example of an obscure sort of Makefile that uses this technique:

A1	= one.c
A2	= two.c

target.o: ${A$(B)}
    $(CC) -c $(CFLAGS) -o $@ $<

Depending on whether the value of the variable B is set to 1 or 2, the target file will depend on one.c or two.c.

Although there is no internal limit restricting the level to which macros can be nested (in the variable name and its value), it is recommended that nesting be kept to a minimum to conserve on memory consumption and stack usage. Each level of nesting requires at least an additional 2K of memory.

Please note that the maximum length of a variable name is limited to 256 characters.