Makefile 102:Variable

Variables (common use)
1
2
3
4
origin_file = hello.c

hellofrommakefile: $(origin_file)
gcc hello.c -o hellofrommakefile
1
2
3
4
5
env := "local"

task1:
@echo "hello ,task1"
@echo "This is environment: $(env)

设置变量 env,如果在执行make时不传值,则使用默认值 local, 输出如下:

1
2
3
4
5
6
7
8
9
# 不给env值
make
hello ,task1
This is env: local

# 给env=uat
make env=uat
hello ,task1
This is env: uat
Automatic Variables(自动变量)

makefile 中有一些预定义的变量,你可以理解它像是 C 语言中的一些关键字,分别有不同的意义,我们列举几个常用的自动变量,其他还有很多

$@:在命令中使用,表示规则中的目标
$<:在命令中使用,表示规则中的第一个条件
$^:在命令中使用,表示规则中的所有条件,组成一个列表,以空格隔开,如果这个列表中有重复的项则消除重复项。

1
2
hellofrommakefile: hello.c
gcc hello.c -o hellofrommakefile

等价于

1
2
hellofrommakefile: hello.c
gcc hello.c -o $@

mplicit Variables(内置变量)

Make命令提供一系列内置变量,比如,$(CC) 指向当前使用的编译器,$(MAKE) 指向当前使用的Make工具。这主要是为了跨平台的兼容性,详细的内置变量清单见ta

More Ref:

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~