Makefile 简明教程

Makefile - Directives

有许多不同形式的指令可用。系统上的 make 程序可能不支持所有指令。因此,请查看你的 make 是否支持我们在此处说明的指令。 GNU make 支持这些指令。

There are numerous directives available in various forms. The make program on your system may not support all the directives. So please check if your make supports the directives we are explaining here. GNU make supports these directives.

Conditional Directives

条件性指令为:

The conditional directives are −

  1. The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared. The lines of the makefile following the ifeq are obeyed if the two arguments match; otherwise they are ignored.

  2. The ifneq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared. The lines of the makefile following the ifneq are obeyed if the two arguments do not match; otherwise they are ignored.

  3. The ifdef directive begins the conditional, and specifies the condition. It contains single argument. If the given argument is true then condition becomes true.

  4. The ifndef directive begins the conditional, and specifies the condition. It contains single argument. If the given argument is false then condition becomes true.

  5. The else directive causes the following lines to be obeyed if the previous conditional failed. In the example above this means the second alternative linking command is used whenever the first alternative is not used. It is optional to have an else in a conditional.

  6. The endif directive ends the conditional. Every conditional must end with an endif.

Syntax of Conditionals Directives

不带 else 的简单条件的语法如下:

The syntax of a simple conditional with no else is as follows −

conditional-directive
   text-if-true
endif

text-if-true 可能为任何文本行,如果条件为真,则被视为 makefile 的一部分。如果条件为假,则不使用任何文本。

The text-if-true may be any lines of text, to be considered as part of the makefile if the condition is true. If the condition is false, no text is used instead.

复杂条件的语法如下:

The syntax of a complex conditional is as follows −

conditional-directive
   text-if-true
else
   text-if-false
endif

如果条件为真,则使用 text-if-true;否则,使用 text-if-false。text-if-false 可以是任意数量的行文本。

If the condition is true, text-if-true is used; otherwise, text-if-false is used. The text-if-false can be any number of lines of text.

无论条件是简单条件还是复杂条件,条件指令的语法都是相同的。有四种不同的指令来测试各种条件。它们如下所述 −

The syntax of the conditional-directive is the same whether the conditional is simple or complex. There are four different directives that test various conditions. They are as given −

ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2"

以上条件的反向指令如下 −

Opposite directives of the above conditions are are follows −

ifneq (arg1, arg2)
ifneq 'arg1' 'arg2'
ifneq "arg1" "arg2"
ifneq "arg1" 'arg2'
ifneq 'arg1' "arg2"

Example of Conditionals Directives

libs_for_gcc = -lgnu
normal_libs =

foo: $(objects)
ifeq ($(CC),gcc)
   $(CC) -o foo $(objects) $(libs_for_gcc)
else
   $(CC) -o foo $(objects) $(normal_libs)
endif

The include Directive

include directive 允许 make 挂起对当前 makefile 的读取并读取一个或多个其他 makefile,然后再继续。该指令是 makefile 中的一行,如下所示 −

The include directive allows make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks follows −

include filenames...

文件名可以包含 shell 文件名模式。行首允许并忽略额外的空格,但不允许制表符。例如,如果你有三个 .mk 文件,即 a.mkb.mkc.mk,并且 $(bar),那么它会扩展到 bish bash,然后是以下表达式。

The filenames can contain shell file name patterns. Extra spaces are allowed and ignored at the beginning of the line, but a tab is not allowed. For example, if you have three `.mk' files, namely, `a.mk', `b.mk', and `c.mk', and $(bar) then it expands to bish bash, and then the following expression.

include foo *.mk $(bar)

is equivalent to:

include foo a.mk b.mk c.mk bish bash

make 处理 include 指令时,它会挂起对 makefile 的读取并依次从每个列出的文件中读取。完成后, make 继续读取出现指令的 makefile。

When the make processes an include directive, it suspends reading of the makefile and reads from each listed file in turn. When that is finished, make resumes reading the makefile in which the directive appears.

The override Directive

如果变量已通过命令参数设置,则忽略 makefile 中的普通赋值。即使变量已通过命令参数设置,你仍希望在 makefile 中设置变量,则可以使用 override 指令,该指令是一行,如下所示−

If a variable has been set with a command argument, then ordinary assignments in the makefile are ignored. If you want to set the variable in the makefile even though it was set with a command argument, you can use an override directive, which is a line that looks follows−

override variable = value

or

override variable := value