阮一峰的网络日志 » 首页 » 档案
2016-06-27 14:34
701 查看
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
activeoldestvotes
add
a comment
![](http://static.adzerk.net/Advertisers/5eb9f89867a24cf6b7c53014e3c15bea.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjo0MDQyODMsImNoIjoxMTc4LCJjayI6e30sImNyIjoxNDM0MzE2LCJkaSI6IjJjNjEwYmU0ZWE2NTQ1Y2RiYTYwMGJkMjk4NTQ0ZWE3IiwiZG0iOjEsImZjIjoxNjQ5MzM2LCJmbCI6MTQ0ODM0MiwiaXAiOiIxNzUuOC4yMC4xNzAiLCJrdyI6Im1ha2VmaWxlLGdudS1tYWtlIiwibnciOjIyLCJwYyI6MCwiZWMiOjAsInByIjoxNjA0LCJydCI6MSwicmYiOiJodHRwOi8vd3d3LnJ1YW55aWZlbmcuY29tL2Jsb2cvMjAxNS8wMi9tYWtlLmh0bWwiLCJzdCI6ODI3NywidWsiOiJ1ZTEtZTBiYWYwZTY2OWMyNGU4OWFlZDRjYjJjZWNmMzZhOGMiLCJ6biI6NDQsInRzIjoxNDY3MDAyNzYyOTIyLCJiZiI6dHJ1ZSwicG4iOiJhZHplcmsxNjQxMTYzMzU0IiwiZnEiOjB9&s=JLIjc2bMqsM_L1bjSnyOT50UbJM)
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
up vote392down votefavorite 191 | 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. makefile gnu-make
| |||
add a comment |
4 Answers
activeoldestvotesup vote515down voteaccepted |
|
shareimprove this answer | edited Aug 25 '15 at 15:04 Eric 3,35131732 | answered Jan 15 '09 at 23:25 Alnitak 205k41263340 |
| Does A += B expand B? That is if if I do A += B, and then B += C, would A evaluate to concatenation of ${B} and ${C}? – mezhaka Feb 1 '13 at 12:46 | ||
| As the linked section of the manual says. += operates according to whatever simple or recursive semantics the original assignment had. So yes, it will expand the RHS but whether it does that immediately or in a deferred manner depends on the type of the variable on the LHS. – Etan Reisner Mar 3 '13 at 21:02 | ||
| What do you mean when you say variable value is expanded? – Сашко Лихенко Feb 20 '15 at 21:45 | ||
@СашкоЛихенко have a look here to get meaning of expansiongnu.org/software/make/manual/make.html#Flavors – Umair R Feb 25 '15 at 9:12 | |||
| is "set if absent" lazy or immediate? can i "lazy set if absent" and "immediate set if abset"? – Woodrow Barlow Apr 1 at 14:55 |
a comment
![](http://static.adzerk.net/Advertisers/5eb9f89867a24cf6b7c53014e3c15bea.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjo0MDQyODMsImNoIjoxMTc4LCJjayI6e30sImNyIjoxNDM0MzE2LCJkaSI6IjJjNjEwYmU0ZWE2NTQ1Y2RiYTYwMGJkMjk4NTQ0ZWE3IiwiZG0iOjEsImZjIjoxNjQ5MzM2LCJmbCI6MTQ0ODM0MiwiaXAiOiIxNzUuOC4yMC4xNzAiLCJrdyI6Im1ha2VmaWxlLGdudS1tYWtlIiwibnciOjIyLCJwYyI6MCwiZWMiOjAsInByIjoxNjA0LCJydCI6MSwicmYiOiJodHRwOi8vd3d3LnJ1YW55aWZlbmcuY29tL2Jsb2cvMjAxNS8wMi9tYWtlLmh0bWwiLCJzdCI6ODI3NywidWsiOiJ1ZTEtZTBiYWYwZTY2OWMyNGU4OWFlZDRjYjJjZWNmMzZhOGMiLCJ6biI6NDQsInRzIjoxNDY3MDAyNzYyOTIyLCJiZiI6dHJ1ZSwicG4iOiJhZHplcmsxNjQxMTYzMzU0IiwiZnEiOjB9&s=JLIjc2bMqsM_L1bjSnyOT50UbJM)
up vote146down vote | 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 = world HELLO_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 = world HELLO_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 = hello HELLO_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 Bis expanded with :=whereas +=would not cause Bto be expanded.
| ||||||||||||||||
a comment |
up vote19down vote | When you use VARIABLE = value, if valueis actually a reference to another variable, then the value is only determined when VARIABLEis used. This is best illustrated with an example: VAL = foo VARIABLE = $(VAL) VAL = bar # VARIABLE and VAL will both evaluate to "bar" When you use VARIABLE := value, you get the value of valueas it is now. For example: VAL = foo VARIABLE := $(VAL) VAL = bar # VAL will evaluate to "bar", but VARIABLE will evaluate to "foo" Using VARIABLE ?= valmeans that you only set the value of VARIABLEif VARIABLEis not set already. If it's not set already, the setting of the value is deferred until VARIABLEis used (as in example 1). VARIABLE += valuejust appends valueto VARIABLE. The actual value of valueis determined as it was when it was initially set, using either =or :=.
| ||||||||||||
a comment |
up vote15down vote | I suggest you do some experiments using "make". Here is a simple demo, showing the difference between =and :=. /* Filename: Makefile*/ x := foo y := $(x) bar x := later a = foo b = $(a) bar a = later test: 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 |
相关文章推荐
- HTTP幂等性
- Ubuntu12.04 上网静态IP配置方法
- LTE 网络架构
- TCP/Protobuf之Netty学习
- C++编程笔记:使用WinHTTP实现HTTP访问(解决接收UTF8数据乱码问题)
- linux网络编程常用头文件
- TCP/Protobuf之基础知识准备
- Android使用VideoView播放网络视频,获取网络视频缩略图
- 奇葩问题:HTTP Status 404
- 目前网络上开源的网络爬虫以及一些简介和比较
- OKHttp请求网络数据
- RTP/RTSP/RTCP有什么区别?
- tcp序列号回绕与解决
- ASP.NET路由系统实现原理:HttpHandler的动态映射
- HTTP图解笔记
- 【转载】浅谈HTTP中Get与Post的区别
- Android网络请求框架剖析一、Okhttp
- 我最喜欢的网络小说
- http长连接和短连接
- android async-http使用