UNIX makefile中的=和:=

原创 2007年09月20日 17:10:00

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 Setting Variables) or by the define directive (see 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 Functions for Transforming Text) referenced in the definition will be executed every time the variable is expanded. This makes make run 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 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 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 Communicating Variables to a Sub-make, for information about MAKELEVEL.)

     ifeq (0,${MAKELEVEL})
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}

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

${MAKE} -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 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 The origin Function):

     ifeq ($(origin FOO), undefined)
FOO = bar

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



介绍一下make及其描述文件makefile Makefile文件         Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间 的相互关系并自动维护编译工作。而ma...
  • sinat_34157462
  • sinat_34157462
  • 2016年04月20日 19:39
  • 1041


疯雨-版权所有,转载请注明【http://blog.csdn.net/u010346967】 经常有人分不清= 、:=和+=的区别  这里我总结下做下详细的分析: 首先你得清楚makefile的运...
  • u010346967
  • u010346967
  • 2015年06月19日 17:00
  • 967


先来看makefile: test1: test1.cpp clean: @echo $(CXX) @echo $(CC) @echo $(AR) @echo $(RM) @echo $(...
  • stpeace
  • stpeace
  • 2016年11月06日 13:22
  • 3586

Makefile 经典教程(看完这篇就够了,不信你拉拉进度条)

支持原创:http://blog.csdn.net/haoel/article/details/2886 makefile很重要 什么是makefile?或许很多Winodws的程序员都不知道这个...
  • mhhyoucom
  • mhhyoucom
  • 2017年09月28日 16:58
  • 538

Makefile 中:= ?= += =的区别 和条件执行

一:在Makefile中经常看到obj-m    := scull.o和KERNELDIR ?= /lib/modules/等不同的赋值方式,现在总结他们的区别: = 是最基本的赋值 := 是覆盖...
  • u012851076
  • u012851076
  • 2014年10月16日 16:12
  • 1858

CMake with Eclipse:UNIX Makefile Generator

File System Setup Eclipse CDT supports projects that already provide a makefile so this is good n...
  • yuleslie
  • yuleslie
  • 2012年04月09日 23:20
  • 1467

[精华] 跟我一起写 Makefile - ChinaUnix.net

  • a568953256
  • a568953256
  • 2013年12月19日 10:55
  • 357

Makefile中的?= := += =的区别

Makefile编写的时候会需要定义一些变量,这时候就需要用到“=”也就是C语言中的赋值符号来进行赋值,但有时候也会遇到":="和"?=",那么它们的区别是什么呢? 举个例子: 编写一个Makefil...
  • m7548352
  • m7548352
  • 2015年11月01日 22:18
  • 1219


方法一:              安装一个MinGW,              安装好后需要设置环境变量,就是把MinGW安装目录下的bin目录设到环境变量中,可以使用命令:           ...
  • ahnselina
  • ahnselina
  • 2013年12月15日 14:42
  • 1645


  • github_38221244
  • github_38221244
  • 2017年06月22日 09:52
  • 451
您举报文章:UNIX makefile中的=和:=