关于程序员:柒变量的-高级主题下

2次阅读

共计 3052 个字符,预计需要花费 8 分钟才能阅读完成。

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

环境变量(全局变量)

  • 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 中定义的变量
    • 局部变量:指定指标的变量
正文完
 0