Linux: Variables (or Macros) in the GNU make Utility

By Emmett Dulaney

The GNU make utility in Linux comes to your rescue by reading and interpreting a makefile. In addition to the basic capability of building targets from dependents, GNU make includes many features that make it easy for you to express the dependencies and rules for building a target from its dependents.

If you need to compile a large number of C++ files by using GCC with the same options, for example, typing the options for each file is tedious. You can avoid this repetitive task by defining a variable or macro in make as follows:

# Define macros for name of compiler
CXX= g++
# Define a macro for the GCC flags
CXXFLAGS= -O2 -g -mcpu=i686
# A rule for building an object file
form.o: form.C form.h
$(CXX) -c $(CXXFLAGS) form.C

In this example, CXX and CXXFLAGS are make variables. (GNU make prefers to call them variables, but most Unix make utilities call them macros.)

To use a variable anywhere in the makefile, start with a dollar sign ($) followed by the variable within parentheses. GNU make replaces all occurrences of a variable with its definition; thus, it replaces all occurrences of $(CXXFLAGS) with the string -O2 -g -mcpu=i686.

GNU make has several predefined variables that have special meanings. This table lists these variables. In addition to the variables listed here, GNU make considers all environment variables (such as PATH and HOME) to be predefined variables as well.

Some Predefined Variables in GNU make
Variable Meaning
$% Member name for targets that are archives. If the target is
libDisp.a(image.o), for example, $% is image.o.
$* Name of the target file without the extension.
$+ Names of all dependent files with duplicate dependencies,
listed in their order of occurrence.
$< The name of the first dependent file.
$? Names of all dependent files (with spaces between the names)
that are newer than the target.
$@ Complete name of the target. If the target is libDisp.a image.o), for example, $@ is libDisp.a.
$^ Names of all dependent files, with spaces between the names.
Duplicates are removed from the dependent filenames.
AR Name of the archive-maintaining program (default value: ar).
ARFLAGS Flags for the archive-maintaining program (default value: rv).
AS Name of the assembler program that converts the assembly
language to object code (default value: as).
ASFLAGS Flags for the assembler.
CC Name of the C compiler (default value: cc).
CFLAGS Flags that are passed to the C compiler.
CO Name of the program that extracts a file from RCS (default
value: co).
COFLAGS Flags for the RCS co program.
CPP Name of the C preprocessor (default value: $(CC) -E).
CPPFLAGS Flags for the C preprocessor.
CXX Name of the C++ compiler (default value: g++).
CXXFLAGS Flags that are passed to the C++ compiler.
FC Name of the FORTRAN compiler (default value: f77).
FFLAGS Flags for the FORTRAN compiler.
LDFLAGS Flags for the compiler when it’s supposed to invoke the
linker ld.
RM Name of the command to delete a file (Default value: rm -f).