GNU make manual 翻译( 一百六十九)

继续翻译

6.10 Variables from the Environment
===================================

Variables in `make' can come from the environment in which `make' is
run.  Every environment variable that `make' sees when it starts up is
transformed into a `make' variable with the same name and value.
However, an explicit assignment in the makefile, or with a command
argument, overrides the environment.  (If the `-e' flag is specified,
then values from the environment override assignments in the makefile.
*Note Summary of Options: Options Summary.  But this is not recommended
practice.)

   Thus, by setting the variable `CFLAGS' in your environment, you can
cause all C compilations in most makefiles to use the compiler switches
you prefer.  This is safe for variables with standard or conventional
meanings because you know that no makefile will use them for other
things.  (Note this is not totally reliable; some makefiles set
`CFLAGS' explicitly and therefore are not affected by the value in the
environment.)

   When `make' runs a recipe, variables defined in the makefile are
placed into the environment of each shell.  This allows you to pass
values to sub-`make' invocations (*note Recursive Use of `make':
Recursion.).  By default, only variables that came from the environment
or the command line are passed to recursive invocations.  You can use
the `export' directive to pass other variables.  *Note Communicating
Variables to a Sub-`make': Variables/Recursion, for full details.

   Other use of variables from the environment is not recommended.  It
is not wise for makefiles to depend for their functioning on
environment variables set up outside their control, since this would
cause different users to get different results from the same makefile.
This is against the whole purpose of most makefiles.

   Such problems would be especially likely with the variable `SHELL',
which is normally present in the environment to specify the user's
choice of interactive shell.  It would be very undesirable for this
choice to affect `make'; so, `make' handles the `SHELL' environment
variable in a special way; see *note Choosing the Shell::.

6.10 来自环境的变量
===================================

make中的变量可以来自于make运行的环境。每一个make所看到的环境变量都传递到一个make变量中,拥有同样的名字和值。但是,在makefile中的显式的复制,或者命令行中的参数,会覆盖掉环境变量中的值。(如果 -e 标志被设定,那么环境中的值会覆盖掉makefile中的赋值。*Note Summary of Options: Options Summary. 但是这不是一个值得推荐的实践)

因此,通过在环境中设置变量 CFLAGS,你可以使大多数的makefie中的所有的C 编译来使用你所喜欢的编译器开关。因为你知道没有makefile 会使用它们来作其他的事情,所在这么做对变量的标准和便利性方面没有问题(注意,这并不是可以完全信赖的,有些makefile通过显式设定 CFLAGS而不受环境变量的影响)。

当make运行一个片段,在makefile中定义的变量会被放入到每一个shell的环境中。着允许你向子make传递变量值(*note Recursive Use of 'make': Recursion.)。缺省的,只有从环境中或者从命令行来的变量才会传递到递归激活的makefile中。你可以使用 export 指令来传递其他的变量。若想要了解更加详细的细节,可以参考*Note Communicating Variables to a Sub-'make': Variables/Recursion。

对来自环境的变量的其他使用时不受推荐的。让makefile依赖它们的功能于处于其控制之外的环境变量是不明智的,因为这将导致不同的用户利用相同的makefile获得不同的结果。这违背了大多数的makefile的目的。

特别是对变量SHELL而言更是如此,它通常是在环境中被提供用来指定用户选择的交互shell。用这个来影响make是极为不妥的;因此,make 处理 SHELL 环境变量的时候采用了特殊的方式;可以参见 *note Choosing the Shell::

后文待续

原文地址:https://www.cnblogs.com/gaojian/p/2711987.html