柒-《变量的 高级主题(下)》

柒- 变量的高级主题(下)

环境变量(全局变量)

  • makefile 中能够直接使用环境变量的值

    • 定义了同名变量,环境变量将被覆盖
    • 运行 make 时指定 '-e' 选项,优先使用环境变量

为什么要在 makefile 中使用环境变量

  • 优势

    • 环境变量可以在所有 makfefile 中使用
  • 劣势

    • 过多的依赖于环境变量会导致移植性降低

变量在不同 makefile 之间的传递方式

不同 makefile 之间变量传递的三种方式!!!

  1. 直接在外部定义环境变量进行传递
  2. 使用export 定义变量进行传递(定义临时环境变量),由于它不会真正修改系统变量的环境变量
  3. 定义 make 命令行变量进行传递(推荐)

编程实验1

JAVA_HOME := java home  
test :
    @echo "JAVA_HOME => $(JAVA_HOME)"

注意
makefile 规定定义的一个变量,这个名字和环境变量名字是一样的话,以当前makefile 定义变量为准,且默认的情况下覆盖环境变量(其他文件表示相同的环境变量名也会覆盖) ,如果不需要这个变量代表环境变量,编译选项make -e ,就可以表示本来的环境变量

编程实验2.1

调用另外一个makefile 文件
$(MAKE) :当前 make 解释器的文件名,make 后面的文件 makefile.2

// 文件makefile
var := Kevin6666
 
test : 
    @echo "test => $(var)"
    @echo "make another file ......"
    @$(MAKE) -f makefile.2
    
//  文件makefile.2 
test : 
    @echo "test => $(var)"
    
// 输出结果  
test => Kevin6666
make another file ......
make[1]: Entering directory '/mnt/hgfs/Fileshare_LinuxC'
test => 
make[1]: Leaving directory '/mnt/hgfs/Fileshare_LinuxC'

具有文件作用域,且不是环境变量

编程实验2.2

// 文件makefile
export var := Kevin6666

test : 
    @echo "test => $(var)"
    @echo "make another file ......"
    @$(MAKE) -f makefile.2

//  文件makefile.2 
test : 
    @echo "test => $(var)"
// 输出  
test => Kevin6666
make another file ......
make[1]: Entering directory '/mnt/hgfs/Fileshare_LinuxC'
test => Kevin6666
make[1]: Leaving directory '/mnt/hgfs/Fileshare_LinuxC'

export 由于它不会真正修改系统变量的环境变量,临时环境变量

编程实验3

// 文件makefile  
var := Kevin6666

test : 
    @echo "test => $(var)"
    @echo "make another file ......"
    @$(MAKE) -f makefile.2 var:=$(var)  

//  文件makefile.2 
test : 
    @echo "test => $(var)"
// 输出  
test => Kevin6666
make another file ......
make[1]: Entering directory '/mnt/hgfs/Fileshare_LinuxC'
test => Kevin6666
make[1]: Leaving directory '/mnt/hgfs/Fileshare_LinuxC'

var:=$(var) 定义命令行环境变量传值

目标变量(局部变量)

目标变量相当于局部变量

  • 作用域只在指定目标及连带规则中

    • target : name<assignment>value
    • target :override name<assignment>value
var := D.T.Software
test : var : test-var  
// test 这个目标,以及依赖定义一个 var 变量,这个变量的值是 test-var 

test :
    @echo "test:"
    @echo "var => $(var)"

编程实验4.1

var := Kevin666

test : var := test-var

test :
    @echo "test :"
    @echo "var => $(var)"
// 输出 
zhabin666@ubuntu:/mnt/hgfs/Fileshare_LinuxC$ make
test :
var => test-var

编程实验4.2

var := Kevin666

test : var := test-var

test : another
    @echo "test :"
    @echo "var => $(var)"
    
another : 
    @echo "another var => $(var)"
// 输出 
another var => test-var
test :
var => test-var

编程实验4.3

var := Kevin666

test : var := test-var

test : 
    @echo "test :"
    @echo "var => $(var)"
    
another : 
    @echo "another var => $(var)"
make test another
// 输出 
zhabin666@ubuntu:/mnt/hgfs/Fileshare_LinuxC$ make test another
test :
var => test-var
another var => Kevin666

模式变量

  • 模式变量是目标变量的扩展
  • 作用域只在符号模式的目标及连带规则中

    • pattern : name<assignment>value
    • pattern :override name<assignment>value
new := Kevin
%e : override new := test-new 

// 匹配以 e结尾的目标

rule :
    @echo "rule:"
    @echo "new => $(new)"

编程实验5

var := D.T.Software
new := TDelphi

test : var := test-var
%e : override new := test-new

test : another
    @echo "test :"
    @echo "var => $(var)"
    @echo "new => $(new)"
    
another :
    @echo "another :"
    @echo "var => $(var)"
    @echo "new => $(new)"
    
rule :
    @echo "rule :"
    @echo "var => $(var)"
    @echo "new => $(new)"

zhabin666@ubuntu:/mnt/hgfs/Fileshare_LinuxC$ make test rule 
another :  
// 输出
var => test-var
new => TDelphi
test :
var => test-var
new => TDelphi
rule :
var => D.T.Software
new => test-new

zhabin666@ubuntu:/mnt/hgfs/Fileshare_LinuxC$ make test rule  new:=cmd_new  

// 输出
another :
var => test-var
new => cmd_new
test :
var => test-var
new => cmd_new
rule :
var => D.T.Software
new => test-new

小结

  • makefile 中的三种变量

    • 全局变量:makefile 外部定义的环境变量
    • 文件变量:makefile 中定义的变量
    • 局部变量:指定目标的变量

评论

暂无评论