Makefile 巨集


make程式允許您使用巨集,這是類似的變數。 = 一對一個Makefile中定義的巨集。例如:

MACROS=  -me
PSROFF=  groff -Tps
DITROFF= groff -Tdvi
CFLAGS= -O -systype bsd43
LIBS = "-lncurses -lm -lsdl"
MYFACE = ":*)"
  • 特殊的巨集

目標規則集發出任何命令之前,有一些特殊的預定義巨集。

因此,舉例來說,我們可以使用一個規則

hello: main.cpp hello.cpp factorial.cpp
	$(CC) $(CFLAGS) $? $(LDFLAGS) -o [email protected]

alternatively:

hello: main.cpp hello.cpp factorial.cpp
        $(CC) $(CFLAGS) [email protected] $(LDFLAGS) -o [email protected]

在這個例子中[email protected]代表 hello, $? 或[email protected]將拾取所有更改的原始檔。

有兩個比較特殊的隱含規則中使用的巨集。它們是

  • $< 導致該操作的相應的檔案中的名稱。

  • $* 字首共用目標和相關檔案。

常見的隱含規則的構造 .o(物件)檔案,.cpp(原始檔)。

.o.cpp:
        $(CC) $(CFLAGS) -c $<

alternatively

.o.cpp:
        $(CC) $(CFLAGS) -c $*.c

注意:要獲得更詳細的關於Makefile規則,在另一部分。

  • 傳統巨集

有很多預設的巨集(輸入“make -p”列印出來的預設值)。大多數使用它們的規則是很明顯的:

這些預定義變數,即。在隱含規則中使用的巨集分為兩大類:那些程式名(例如CC)和那些含有引數的程式(如CFLAGS)。

下面是一些比較常見的變數用作內建規則:makefile檔案的程式名稱的表。

AR Archive-maintaining program; default `ar'.
AS Program for compiling assembly files; default `as'.
CC Program for compiling C programs; default `cc'.
CO Program for checking out files from RCS; default `co'.
CXX Program for compiling C++ programs; default `g++'.
CPP Program for running the C preprocessor, with results to standard output; default `$(CC) -E'.
FC Program for compiling or preprocessing Fortran and Ratfor programs; default `f77'.
GET Program for extracting a file from SCCS; default `get'.
LEX Program to use to turn Lex grammars into source code; default `lex'.
YACC Program to use to turn Yacc grammars into source code; default `yacc'.
LINT Program to use to run lint on source code; default `lint'.
M2C Program to use to compile Modula-2 source code; default `m2c'.
PC Program for compiling Pascal programs; default `pc'.
MAKEINFO Program to convert a Texinfo source file into an Info file; default `makeinfo'.
TEX Program to make TeX dvi files from TeX source; default `tex'.
TEXI2DVI Program to make TeX dvi files from Texinfo source; default `texi2dvi'.
WEAVE Program to translate Web into TeX; default `weave'.
CWEAVE Program to translate C Web into TeX; default `cweave'.
TANGLE Program to translate Web into Pascal; default `tangle'.
CTANGLE Program to translate C Web into C; default `ctangle'.
RM Command to remove a file; default `rm -f'.

這裡是一個變數,其值是上述程式的額外的引數表。所有這些的預設值是空字串,除非另有說明。

ARFLAGS Flags to give the archive-maintaining program; default `rv'.
ASFLAGS Extra flags to give to the assembler (when explicitly invoked on a `.s' or `.S' file).
CFLAGS Extra flags to give to the C compiler.
CXXFLAGS Extra flags to give to the C compiler.
COFLAGS Extra flags to give to the RCS co program.
CPPFLAGS Extra flags to give to the C preprocessor and programs that use it (the C and Fortran compilers).
FFLAGS Extra flags to give to the Fortran compiler.
GFLAGS Extra flags to give to the SCCS get program.
LDFLAGS Extra flags to give to compilers when they are supposed to invoke the linker, `ld'.
LFLAGS Extra flags to give to Lex.
YFLAGS Extra flags to give to Yacc.
PFLAGS Extra flags to give to the Pascal compiler.
RFLAGS Extra flags to give to the Fortran compiler for Ratfor programs.
LINTFLAGS Extra flags to give to lint.

註:您可以取消`-R'或` --no-builtin-variables“選項隱含規則使用的所有變數。

如,也可以在命令列中定義的巨集

          make CPP = /home/courses/cop4530/spring02