继续翻译
7 Conditional Parts of Makefiles ******************************** A "conditional" directive causes part of a makefile to be obeyed or ignored depending on the values of variables. Conditionals can compare the value of one variable to another, or the value of a variable to a constant string. Conditionals control what `make' actually "sees" in the makefile, so they _cannot_ be used to control recipes at the time of execution. 7.1 Example of a Conditional ============================ The following example of a conditional tells `make' to use one set of libraries if the `CC' variable is `gcc', and a different set of libraries otherwise. It works by controlling which of two recipe lines will be used for the rule. The result is that `CC=gcc' as an argument to `make' changes not only which compiler is used but also which libraries are linked. 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 This conditional uses three directives: one `ifeq', one `else' and one `endif'. 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. The `else' directive causes the following lines to be obeyed if the previous conditional failed. In the example above, this means that the second alternative linking command is used whenever the first alternative is not used. It is optional to have an `else' in a conditional. The `endif' directive ends the conditional. Every conditional must end with an `endif'. Unconditional makefile text follows.
7 makefile的条件式部分
********************************
一个conditional 指令导致 makefile的一部分被遵守或者被忽略,这依赖于变量的值。条件式可以比较两个变量的值,或者一个变量和另一个常量的值。条件式控制make 实际在makefile中看到的东西,因此不能用来在执行的时候控制片段。
7.1 条件式的例子
============================
如下的条件式的例子告诉make ,如果CC 变量是 gcc, 则使用 一套库,否则使用另一套库。它通过控制哪两行片段行在规则中被使用而工作。结果是通过 CC=gcc的参数,make 不仅改变了所用的编译器而且也改变了所要链接的库。
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
这个条件式使用了三个指令:一个 ifeq,一个 else,一个 endif。
ifeq 指令开启了条件式并且制定了条件。它包含两个参数,用逗号分割,用口号包围。变量替换发生在各个参数,然后参数被比较。如果两个参数匹配,则ifeq 后的行被遵守,否则被忽略。
else指令导致如果前面的条件失败了,则此后的行得到执行。在上面的例子里,这意味着如果第一个命令没有被使用,则第二个后备链接命令被使用。在条件是中else 是可选的。
endif 指令结束条件式。每一个条件式必须以endif 来结束。后面跟着的是没有非条件式makefile文本。
后文待续