最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Makefile中变量赋值时冒号加等号:=是什么含义

makefile crifan 6468浏览 0评论

折腾:

【已解决】Makefile中如何实现make时给变量传递参数如果没传用默认值

期间,之前就想要去搞清楚:

variable1 = value1

和:

variable1 := value1

的区别。

正好搜到:

GNU make – How to Use Variables

就去好好看看,两者区别。

The Two Flavors of Variables

There are two ways that a variable in GNU make can have a value; we call them the two flavors of variables. The two flavors are distinguished in how they are defined and in what they do when expanded.

The first flavor of variable is a recursively expanded variable. Variables of this sort are defined by lines using `=’ (see section Setting Variables) or by the define directive (see section Defining Variables Verbatim). The value you specify is installed verbatim; if it contains references to other variables, these references are expanded whenever this variable is substituted (in the course of expanding some other string). When this happens, it is called recursive expansion.

For example,

foo = $(bar)

bar = $(ugh)

ugh = Huh?

all:;echo $(foo)

will echo `Huh?’`$(foo)’ expands to `$(bar)’ which expands to `$(ugh)’ which finally expands to `Huh?’.

This flavor of variable is the only sort supported by other versions of make. It has its advantages and its disadvantages. An advantage (most would say) is that:

CFLAGS = $(include_dirs) -O

include_dirs = -Ifoo -Ibar

will do what was intended: when `CFLAGS’ is expanded in a command, it will expand to `-Ifoo -Ibar -O’. A major disadvantage is that you cannot append something on the end of a variable, as in

CFLAGS = $(CFLAGS) -O

because it will cause an infinite loop in the variable expansion. (Actually make detects the infinite loop and reports an error.)

Another disadvantage is that any functions (see section Functions for Transforming Text) referenced in the definition will be executed every time the variable is expanded. This makes makerun slower; worse, it causes the wildcard and shell functions to give unpredictable results because you cannot easily control when they are called, or even how many times.

To avoid all the problems and inconveniences of recursively expanded variables, there is another flavor: simply expanded variables.

Simply expanded variables are defined by lines using `:=’ (see section Setting Variables). The value of a simply expanded variable is scanned once and for all, expanding any references to other variables and functions, when the variable is defined. The actual value of the simply expanded variable is the result of expanding the text that you write. It does not contain any references to other variables; it contains their values as of the time this variable was defined. Therefore,

x := foo

y := $(x) bar

x := later

is equivalent to

y := foo bar

x := later

When a simply expanded variable is referenced, its value is substituted verbatim.

Here is a somewhat more complicated example, illustrating the use of `:=’ in conjunction with the shell function. (See section The shell Function.) This example also shows use of the variable MAKELEVEL, which is changed when it is passed down from level to level. (See section Communicating Variables to a Sub-make, for information about MAKELEVEL.)

ifeq (0,${MAKELEVEL})

cur-dir   := $(shell pwd)

whoami    := $(shell whoami)

host-type := $(shell arch)

MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}

endif

An advantage of this use of `:=’ is that a typical `descend into a directory’ command then looks like this:

${subdirs}:

      ${MAKE} cur-dir=${cur-dir}/$@ -C $@ all

Simply expanded variables generally make complicated makefile programming more predictable because they work like variables in most programming languages. They allow you to redefine a variable using its own value (or its value processed in some way by one of the expansion functions) and to use the expansion functions much more efficiently (see section Functions for Transforming Text).

You can also use them to introduce controlled leading whitespace into variable values. Leading whitespace characters are discarded from your input before substitution of variable references and function calls; this means you can include leading spaces in a variable value by protecting them with variable references, like this:

nullstring :=

space := $(nullstring) # end of the line

Here the value of the variable space is precisely one space. The comment `# end of the line’ is included here just for clarity. Since trailing space characters are not stripped from variable values, just a space at the end of the line would have the same effect (but be rather hard to read). If you put whitespace at the end of a variable value, it is a good idea to put a comment like that at the end of the line to make your intent clear. Conversely, if you do not want any whitespace characters at the end of your variable value, you must remember not to put a random comment on the end of the line after some whitespace, such as this:

dir := /foo/bar    # directory to put the frobs in

Here the value of the variable dir is `/foo/bar ‘ (with four trailing spaces), which was probably not the intention. (Imagine something like `$(dir)/file’ with this definition!)

There is another assignment operator for variables, `?=’. This is called a conditional variable assignment operator, because it only has an effect if the variable is not yet defined. This statement:

FOO ?= bar

is exactly equivalent to this (see section The origin Function):

ifeq ($(origin FOO), undefined)

  FOO = bar

endif

Note that a variable set to an empty value is still defined, so `?=’ will not set that variable.

【总结】

Makefile中:

variable1 = value1

和:

variable1 := value1

是两种不同的赋值,基本区别是:

Makefile中赋值方式写法含义优点缺点
recursively expanded variables=递归展开的值
variable1 = value1
递归式的展开变量的值
支持后续的递归变量的定义和展开
不支持递归变量定义;
每次变量被用到时都会被展开计算一次,效率较低,速度较慢;
使得变量的赋值,在用到wildcard和shell等时候,很难预测
Simply expanded variables=简单展开的值
variable1 := value1
只是在变量被赋值时展开一次变量赋值就像其他编程语言中的赋值一样,变得可预测,尤其是在

举例:

(1)recursively expanded variables

支持:

CFLAGS = $(include_dirs) -O

include_dirs = -Ifoo -Ibar

不支持:

CFLAGS = $(CFLAGS) -O

->Make会报递归定义的错误:

Makefile:238: *** Recursive variable `CFLAGS’ references itself (eventually).  Stop.

(2)Simply expanded variables

支持:

CFLAGS := $(CFLAGS) -O

debug_var:

  @echo CFLAGS=$(CFLAGS)

调用:

CFLAGS=-Ixxx make debug_var

输出效果:

CFLAGS=-Ixxx -O

支持:用如下写法:

ifeq (0,${MAKELEVEL})

cur-dir   := $(shell pwd)

whoami    := $(shell whoami)

host-type := $(shell arch)

MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}

endif

${subdirs}:

      ${MAKE} cur-dir=${cur-dir}/$@ -C $@ all

实现很方便的循环遍历去进入子文件夹去make。

【后记】

但是对于递归调用展开的话,对于:

cur_dir = $(shell pwd)

after_cd_cur_dir = $(shell pwd)

debug_var:

  @echo CFLAGS=$(CFLAGS) && \

  echo before cd cur_dir=$(cur_dir) && \

  cd $(GITHUB_IO_PATH) && \

  pwd && \

  echo “after cd cur_dir=$(after_cd_cur_dir)”

却输出:

CFLAGS=-Ixxx -O

before cd cur_dir=/Users/crifan/GitBook/Library/Import/http_summary

/Users/crifan/dev/dev_root/github/github.io/crifan.github.io

after cd cur_dir=/Users/crifan/GitBook/Library/Import/http_summary

即:after_cd_cur_dir却还是当前文件夹,表示不是很理解。以为应该是cd后的:

/Users/crifan/dev/dev_root/github/github.io/crifan.github.io

呢。以后再深入研究吧。

转载请注明:在路上 » 【已解决】Makefile中变量赋值时冒号加等号:=是什么含义

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.156 seconds, using 22.00MB memory