预定义变量的使用

来源:互联网 发布:pdf expert mac破解版 编辑:程序博客网 时间:2024/06/08 10:21

1 在makefile中存在一些预定义的变量

自动变量

  • $@, $^, $<

特殊变量

  • $(MAKE), $(MAKECMDGOALS), $(MAKEFILE_LIST)
  • $(MAKE_VERSION), $(CURDIR), $(.VARIABLES)
  • 。。。

2 自动变量的意义

$@

  • 当前规则中触发命令被执行的目标

$^

  • 当前规则中的所有依赖

$<

  • 当前规则中的第一个依赖

自动变量的使用示例

这里写图片描述

注意

  • $”对于makefile有特殊含义,输出时需要加上一个”$”进行转义
  • “$@”对于Bash Shell有特殊含义,输出时需要加上”\”进行转义

编程实验:自动变量的使用

.PHONY : all first second third all : first second third    @echo "\$$@ => $@"    @echo "$$^ => $^"@echo "$$< => $<"firtst:second:third:
CC := g++TARGET := hello-world.out$(TARGET) : func.o main.o    $(CC) -o $@ $^func.o : func.c    $(CC) -o $@ -c $^main.o : main.c    $(CC) -o $@ -c $^.PHONY : rebuild clean allrebuild : clean allall : $(TARGET)clean :    $(RM) *.o $(TARGET)

3 特殊变量的含义

$(MAKE)

  • 当前make解释器的文件名

$(MAKECMDGOALS)

  • 命令行中指定的目标名(make的命令行参数)

$(MAKEFILE_LIST)

  • make所需处理的makefile文件列表
  • 当前makefile的文件名总是位于列表的最后
  • 文件名之间以空格进行分隔

编程实验:预定义变量示例一

.PHONY : all out first second third testall out :     @echo "$(MAKE)"    @echo "$(MAKECMDGOALS)"    @echo "$(MAKEFILE_LIST)"first :    @echo "first"second :    @echo "second"third :    @echo "third"test :    @$(MAKE) first    @$(MAKE) second    @$(MAKE) thirdmake all的执行结果:makeall makefile(注意:这个前面有一个空格)make test的执行结果:make[1]: Entering directory `/home/aston/comeon'firstmake[1]: Leaving directory `/home/aston/comeon'make[1]: Entering directory `/home/aston/comeon'secondmake[1]: Leaving directory `/home/aston/comeon'make[1]: Entering directory `/home/aston/comeon'thirdmake[1]: Leaving directory `/home/aston/comeon'

$(MAKE_VERSION)

  • 当前make解释器的版本

$(CURDIR)

  • 当前make解释器的工作目录

$(.VARIABLES)

  • 所有已经定义的变量名列表(预定义变量和自定义变量)

编程实验:预定义变量示例二

.PHONY : test1 test2TDelphi := Delphi TangD.T.Software := D.T.test1 :    @echo "$(MAKE_VERSION)"    @echo "$(CURDIR)"    @echo "$(.VARIABLES)"test2 :    @echo "$(RM)"make test2的输出结果:rm -f

注意:@echo “$(.VARIABLES)”,必须用“”引起来,否则会报“/bin/sh: 1: cannot open D: No such file”

4 小结

  • makefile提供了预定义变量供开发者使用
  • 预定义变量的使用能够使得makefile的开发更加高效
  • 自动变量是makefile中最常见的元素
  • 使用$(.VARIABLES)能够获取所有的特殊变量

修改记录


时间 动作 2017.7.9 首次上传

参考资料


唐老师 — 狄泰软件学院 — 十二月提升计划

原创粉丝点击