阮一峰的网络日志 » 首页 » 档案
来源:互联网 发布:h5开心农场源码 编辑:程序博客网 时间:2024/05/15 10:30
http://www.ruanyifeng.com/blog/developer/
http://www.ruanyifeng.com/blog/developer/
http://www.ruanyifeng.com/blog/developer/
http://www.ruanyifeng.com/blog/developer/
http://stackoverflow.com/questions/448910/makefile-variable-assignment
Makefile variable assignment
Can anybody give a clear explanation of how variable assignment really works in Makefiles.
What is the difference between :
VARIABLE = value VARIABLE ?= value VARIABLE := value VARIABLE += value
I have read the section in GNU Make's manual, but it still doesn't make sense to me.
4 Answers
Lazy Set
VARIABLE = value
Normal setting of a variable - values within it are recursively expanded when the variable is used, not when it's declared
Immediate Set
VARIABLE := value
Setting of a variable with simple expansion of the values inside - values within it are expanded at declaration time.
Set If Absent
VARIABLE ?= value
Setting of a variable only if it doesn't have a value
Append
VARIABLE += value
Appending the supplied value to the existing value (or setting to that value if the variable didn't exist)
Using =
causes the variable to be assigned a value. If the variable already had a value, it is replaced. This value will be expanded when it is used. For example:
HELLO = worldHELLO_WORLD = $(HELLO) world!# This echoes "world world!"echo $(HELLO_WORLD)HELLO = hello# This echoes "hello world!"echo $(HELLO_WORLD)
Using :=
is similar to using =
. However, instead of the value being expanded when it is used, it is expanded during the assignment. For example:
HELLO = worldHELLO_WORLD := $(HELLO) world!# This echoes "world world!"echo $(HELLO_WORLD)HELLO = hello# Still echoes "world world!"echo $(HELLO_WORLD)HELLO_WORLD := $(HELLO) world!# This echoes "hello world!"echo $(HELLO_WORLD)
Using ?=
assigns the variable a value iff the variable was not previously assigned. If the variable was previously assigned a blank value (VAR=
), it is still considered set I think. Otherwise, functions exactly like =
.
Using +=
is like using =
, but instead of replacing the value, the value is appended to the current one, with a space in between. If the variable was previously set with :=
, it is expanded I think. The resulting value is expanded when it is used I think. For example:
HELLO_WORLD = helloHELLO_WORLD += world!# This echoes "hello world!"echo $(HELLO_WORLD)
If something like HELLO_WORLD = $(HELLO_WORLD) world!
were used, recursion would result, which would most likely end the execution of your Makefile. If A := $(A) $(B)
were used, the result would not be the exact same as using +=
because B
is expanded with :=
whereas +=
would not cause B
to be expanded.
VARIABLE = literal
and VARIABLE := literal
are always equivalent. Did I get that right? – aiao May 3 '14 at 21:43 When you use VARIABLE = value
, if value
is actually a reference to another variable, then the value is only determined when VARIABLE
is used. This is best illustrated with an example:
VAL = fooVARIABLE = $(VAL)VAL = bar# VARIABLE and VAL will both evaluate to "bar"
When you use VARIABLE := value
, you get the value of value
as it is now. For example:
VAL = fooVARIABLE := $(VAL)VAL = bar# VAL will evaluate to "bar", but VARIABLE will evaluate to "foo"
Using VARIABLE ?= val
means that you only set the value of VARIABLE
if VARIABLE
is not set already. If it's not set already, the setting of the value is deferred until VARIABLE
is used (as in example 1).
VARIABLE += value
just appends value
to VARIABLE
. The actual value of value
is determined as it was when it was initially set, using either =
or :=
.
I suggest you do some experiments using "make". Here is a simple demo, showing the difference between =
and :=
.
/* Filename: Makefile*/x := fooy := $(x) barx := latera = foob = $(a) bara = latertest: echo x - $(x) echo y - $(y) echo a - $(a) echo b - $(b)
Now, save the file and type make test
, I think that would answer your question.
Check more elaborate explanation here
- 阮一峰的网络日志 » 首页 » 档案
- 阮一峰的网络日志地址
- 阮一峰的网络日志
- 阮一峰的网络日志
- 阮一峰的网络日志
- 阮一峰的网络日志 | 很好的jquery总结
- zz 回车和换行 - 阮一峰的网络日志
- 第一次的网络日志
- 洪波的网络日志
- SONG_HCVT网络日志的内容简介
- 网络日志的地址
- 抓网络日志的方法
- 阅读阮一峰网络日志笔记
- 侯捷的职业建议(转自阮一峰的网络日志)
- 进程与线程的一个简单解释---阮一峰的网络日志
- 创业公司打工指南[转自阮一峰的网络日志]
- 【转】世界海底光缆分布图(摘自阮一峰的网络日志)
- 谈谈MVC模式——转自阮一峰的网络日志
- MyEclipse、eclipse代码自动补全(联想输入)
- 灌云伊山人口年龄性别分布图
- Thymeleaf 详解
- Java IO
- 一个优秀的数据产品经理是怎么炼成的?
- 阮一峰的网络日志 » 首页 » 档案
- LightOJ 1104 Birthday Paradox
- windows性能监视器常用计数器
- HTTP、TCP/IP协议与socket之间的区别
- iOS 截图(包含statusbar)
- 剑指offer38--数字的和为某个数
- 中断上下文
- 腾讯首次公布,H5平均分享率只有3.93%
- ios备忘录