2.2 Variables


2.2 Variables

Let's look at some of the variables we have been using in our examples. The simplest ones have the syntax:

 $(   variable-name   ) 

This indicates that we want to expand the variable whose name is variable-name . Variables can contain almost any text, and variable names can contain most characters including punctuation. The variable containing the C compile command is COMPILE.c , for example. In general, a variable name must be surrounded by $( ) to be recognized by make . As a special case, a single character variable name does not require the parentheses.

A makefile will typically define many variables, but there are also many special variables defined automatically by make . Some can be set by the user to control make 's behavior while others are set by make to communicate with the user's makefile .

2.2.1 Automatic Variables

Automatic variables are set by make after a rule is matched. They provide access to elements from the target and prerequisite lists so you don't have to explicitly specify any filenames. They are very useful for avoiding code duplication, but are critical when defining more general pattern rules (discussed later).

There are six " core " automatic variables:


The filename representing the target.


The filename element of an archive member specification.


The filename of the first prerequisite.


The names of all prerequisites that are newer than the target, separated by spaces.


The filenames of all the prerequisites, separated by spaces. This list has duplicate filenames removed since for most uses, such as compiling, copying, etc., duplicates are not wanted.


Similar to $^ , this is the names of all the prerequisites separated by spaces, except that $+ includes duplicates. This variable was created for specific situations such as arguments to linkers where duplicate values have meaning.


The stem of the target filename. A stem is typically a filename without its suffix. (We'll discuss how stems are computed later in Section 2.4.) Its use outside of pattern rules is discouraged.

In addition, each of the above variables has two variants for compatibility with other make s. One variant returns only the directory portion of the value. This is indicated by appending a "D" to the symbol, $(@D) , $(<D) , etc. The other variant returns only the file portion of the value. This is indicated by appending an F to the symbol, $(@F) , $(<F) , etc. Note that these variant names are more than one character long and so must be enclosed in parentheses. GNU make provides a more readable alternative with the dir and notdir functions. We will discuss functions in Chapter 4.

Automatic variables are set by make after a rule has been matched with its target and prerequisites so the variables are only available in the command script of a rule.

Here is our makefile with explicit filenames replaced by the appropriate automatic variable.

 count_words: count_words.o counter.o lexer.o -lfl         gcc $^ -o $@ count_words.o: count_words.c         gcc -c $< counter.o: counter.c         gcc -c $< lexer.o: lexer.c         gcc -c $< lexer.c: lexer.l         flex -t $< > $@ 

Managing Projects with GNU make
Managing Projects with GNU Make (Nutshell Handbooks)
ISBN: 0596006101
EAN: 2147483647
Year: 2003
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net