Makefile中的变量扩展问题

make执行的两个阶段:

In the first phase, make reads the makefile and any included makefiles. At this time, variables and rules are loaded into make’s internal database and the dependency graph is created. In the second phase, make analyzes the dependency graph and determines the targets that need to be updated, then executes command scripts to perform the required updates.

 

关于makefile中的变量何时被扩展的总结:

To summarize, here are the rules for when elements of a makefile are expanded:
• For variable assignments, the lefthand side of the assignment is always expanded immediately when make reads the line during its first phase.
• The righthand side of = and ?= are deferred until they are used in the second phase.
• The righthand side of := is expanded immediately.
• The righthand side of += is expanded immediately if the lefthand side was originally defined as a simple variable. Otherwise, its evaluation is deferred.
• For macro definitions (those using define), the macro variable name is immediately expanded and the body of the macro is deferred until used.
• For rules, the targets and prerequisites are always immediately expanded while the commands are always deferred.

 

Definition Expansion of a Expansion of b
a = b Immediate Deferred
a ?= b Immediate Deferred
a := b Immediate Immediate
a += b Immediate Deferred or immediate
define a
  b...
  b...
  b...
endef
Immediate Deferred

posted on 2010-09-22 23:33  胡是  阅读(633)  评论(0编辑  收藏  举报

导航