makfile 中 $$ 变量的含义

来源:互联网 发布:mac语言设置 编辑:程序博客网 时间:2024/05/21 12:41
例子:

dep:
    sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
    (for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_make    #
    cp tmp_make Makefile
    (cd fs; make dep)
    (cd kernel; make dep)
    (cd mm; make dep)


其中的 $$i 好长时间让我无法理解,最近看到一个关于 makefile 的文档, 它的解释是这样的:

出现在规则命令行中shell变量(一般为执行命令过程中的临时变量,它不属于Makefile变量,而是一个shell变量)引用使用shell的“$tmp”格式。

这也就是说, 在
(for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_make
中的 i 是一个 shell 临时变量。在 shell 是引用变量与makefile中引用变是一样也是用 $符。所以 $i也就代表的是一个 shell 对i变量的引用。

那么再在makefile中引用这个 shell变量,所以就成了 $($i)。而在makefile中可以将()去掉,最终就变成了 $$i.

 =  与:=  区别 
 两者都用于变量赋值,:=是简单赋值,=带递归引用 
 如= 后面是变量, 则变量本身值可能还要进行运算 
 :=  只是简单赋值, 后面变量只简单取值, 没有定义变量取为空, 一般比较安全 
 假设CFLAGS 预先定义有值, 则CFLAGS = $(CFLAGS)-g 变成无限递归调用, 最后变成最终堆栈溢出,正确写法是CFLAGS :=
$(CFLAGS) -g
  #这里foo最终值为Huh,因为foo后面bar还要做递归调用,而ugh也会做递归调用 
                   foo = $(bar) 
                   bar = $(ugh) 
                   ugh = Huh? 
 #这里foo最终值为空,定义foo时,bar并未定义,这里取空. 
     foo := $(bar) 
                   bar := $(ugh) 
                   ugh := Huh?