The make program allows you to use macros, which are similar to variables. Macros are defined in a Makefile as = pairs. An example has been shown below −
MACROS = -me PSROFF = groff -Tps DITROFF = groff -Tdvi CFLAGS = -O -systype bsd43 LIBS = "-lncurses -lm -lsdl" MYFACE = ":*)"
Before issuing any command in a target rule set, there are certain special macros predefined −
$@ is the name of the file to be made.
$? is the names of the changed dependents.
For example, we could use a rule as follows −
hello: main.cpp hello.cpp factorial.cpp $(CC) $(CFLAGS) $? $(LDFLAGS) -o $@ Alternatively: hello: main.cpp hello.cpp factorial.cpp $(CC) $(CFLAGS) $@.cpp $(LDFLAGS) -o $@
In this example, $@ represents hello and $? or $@.cpp picks up all the changed source files.
There are two more special macros used in the implicit rules. They are −
$< the name of the related file that caused the action.
$* the prefix shared by target and dependent files.
Common implicit rule is for the construction of .o (object) files out of .cpp (source files).
.cpp.o: $(CC) $(CFLAGS) -c $< Alternatively: .cpp.o: $(CC) $(CFLAGS) -c $*.c
There are various default macros. You can see them by typing "make -p" to print out the defaults. Most are pretty obvious from the rules in which they are used.
These predefined variables, i.e., macros used in implicit rules fall into two classes. They are as follows −
Macros that are names of programs (such as CC)
Macros that contain arguments of the programs (such as CFLAGS).
Below is a table of some of the common variables used as names of programs in built-in rules of makefiles −
|Sr.No||Variables & Description|
Archive-maintaining program; default is `ar'.
Program to compiling assembly files; default is `as'.
Program to compiling C programs; default is `cc'.
Program to checking out files from RCS; default is `co'.
Program to compiling C++ programs; default is `g++'.
Program to running the C preprocessor, with results to standard output; default is `$(CC) -E'.
Program to compiling or preprocessing Fortran and Ratfor programs; default is `f77'.
Program to extract a file from SCCS; default is `get'.
Program to use to turn Lex grammars into source code; default is `lex'.
Program to use to turn Yacc grammars into source code; default is `yacc'.
Program to use to run lint on source code; default is `lint'.
Program to use to compile Modula-2 source code; default is `m2c'.
Program for compile Pascal programs; default is `pc'.
Program to convert a Texinfo source file into an Info file; default is `makeinfo'.
Program to make TeX dvi files from TeX source; default is `tex'.
Program to make TeX dvi files from Texinfo source; default is `texi2dvi'.
Program to translate Web into TeX; default is `weave'.
Program to translate C Web into TeX; default is `cweave'.
Program to translate Web into Pascal; default is `tangle'.
Program to translate C Web into C; default is `ctangle'.
Command to remove a file; default is `rm -f'.
Here is a table of variables whose values are additional arguments for the programs above. The default values for all of these is the empty string, unless otherwise noted.
|Sr.No.||Variables & Description|
Flags to give the archive-maintaining program; default is `rv'.
Extra flags to give to the assembler when explicitly invoked on a `.s' or `.S' file.
Extra flags to give to the C compiler.
Extra flags to give to the C compiler.
Extra flags to give to the RCS co program.
Extra flags to give to the C preprocessor and programs, which use it (such as C and Fortran compilers).
Extra flags to give to the Fortran compiler.
Extra flags to give to the SCCS get program.
Extra flags to give to compilers when they are supposed to invoke the linker, `ld'.
Extra flags to give to Lex.
Extra flags to give to Yacc.
Extra flags to give to the Pascal compiler.
Extra flags to give to the Fortran compiler for Ratfor programs.
Extra flags to give to lint.
NOTE − You can cancel all variables used by implicit rules with the '-R' or '--no-builtin-variables' option.
You can also define macros at the command line as shown below −
make CPP = /home/courses/cop4530/spring02