Makefile 简明教程

Makefile - Macros

make 程序允许您使用类似于变量的宏。宏在 Makefile 中定义为 = 对。以下展示了一个示例:

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  = ":*)"

Special Macros

在目标规则集中发出任何命令之前,会预定义某些特殊宏:

Before issuing any command in a target rule set, there are certain special macros predefined −

  1. $@ is the name of the file to be made.

  2. $? 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 $@

在此示例中,$@ 代表 hello,$? 或 $@.cpp 获取所有已更改的源文件。

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 −

  1. $< the name of the related file that caused the action.

  2. $* the prefix shared by target and dependent files.

常见的隐式规则是使用 .cpp(源文件)生成 .o(对象)文件。

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

Conventional Macros

有各种默认宏。您可以通过键入“make -p”来打印默认值来查看它们。大多数都是从它们所用的规则中显而易见的。

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 −

  1. Macros that are names of programs (such as CC)

  2. Macros that contain arguments of the programs (such as CFLAGS).

以下是 makefile 内置规则中用作程序名称的一些常见变量的表格−

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

1

AR Archive-maintaining program; default is `ar'.

2

AS Program to compiling assembly files; default is `as'.

3

CC Program to compiling C programs; default is `cc'.

4

CO Program to checking out files from RCS; default is `co'.

5

CXX Program to compiling C programs; default is `g'.

6

CPP Program to running the C preprocessor, with results to standard output; default is `$(CC) -E'.

7

FC Program to compiling or preprocessing Fortran and Ratfor programs; default is `f77'.

8

GET Program to extract a file from SCCS; default is `get'.

9

LEX Program to use to turn Lex grammars into source code; default is `lex'.

10

YACC Program to use to turn Yacc grammars into source code; default is `yacc'.

11

LINT Program to use to run lint on source code; default is `lint'.

12

M2C Program to use to compile Modula-2 source code; default is `m2c'.

13

PC Program for compile Pascal programs; default is `pc'.

14

MAKEINFO Program to convert a Texinfo source file into an Info file; default is `makeinfo'.

15

TEX Program to make TeX dvi files from TeX source; default is `tex'.

16

TEXI2DVI Program to make TeX dvi files from Texinfo source; default is `texi2dvi'.

17

WEAVE Program to translate Web into TeX; default is `weave'.

18

CWEAVE Program to translate C Web into TeX; default is `cweave'.

19

TANGLE Program to translate Web into Pascal; default is `tangle'.

20

CTANGLE Program to translate C Web into C; default is `ctangle'.

21

RM 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

1

ARFLAGS Flags to give the archive-maintaining program; default is `rv'.

2

ASFLAGS Extra flags to give to the assembler when explicitly invoked on a `.s' or `.S' file.

3

CFLAGS Extra flags to give to the C compiler.

4

CXXFLAGS Extra flags to give to the C compiler.

5

COFLAGS Extra flags to give to the RCS co program.

6

CPPFLAGS Extra flags to give to the C preprocessor and programs, which use it (such as C and Fortran compilers).

7

FFLAGS Extra flags to give to the Fortran compiler.

8

GFLAGS Extra flags to give to the SCCS get program.

9

LDFLAGS Extra flags to give to compilers when they are supposed to invoke the linker, `ld'.

10

LFLAGS Extra flags to give to Lex.

11

YFLAGS Extra flags to give to Yacc.

12

PFLAGS Extra flags to give to the Pascal compiler.

13

RFLAGS Extra flags to give to the Fortran compiler for Ratfor programs.

14

LINTFLAGS Extra flags to give to lint.

NOTE − 使用 -R'' 或 --no-builtin-variables'' 选项,可以取消隐式规则使用的所有变量。

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