2.4 Makefile中使用变量

Variables Make Makefiles Simpler

一、本节概要

Variables Make Makefiles Simpler (变量使Makefile更简单),以下是官方给出的原文,接下来会对本节内容进行拆解,并给出详细示例代码。

In our example, we had to list all the object files twice in the rule for edit (repeated here):
edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o


Such duplication is error-prone; if a new object file is added to the system, we might
add it to one list and forget the other. We can eliminate the risk and simplify the makefile
by using a variable. Variables allow a text string to be defined once and substituted in
multiple places later (see Chapter 6 [How to Use Variables], page 65).
It is standard practice for every makefile to have a variable named objects, OBJECTS,
objs, OBJS, obj, or OBJ which is a list of all object file names. We would define such a
variable objects with a line like this in the makefile:
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
Then, each place we want to put a list of the object file names, we can substitute the
variable’s value by writing ‘$(objects)’ (see Chapter 6 [How to Use Variables], page 65).
Here is how the complete simple makefile looks when you use a variable for the object
files:



Chapter 2: An Introduction to Makefiles
7
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit $(objects)

二 、Makefile中使用变量

2.1 未使用变量时的Makefile

在未使用变量时,我们需要手动写出每一个依赖的文件名(在目标后面的依赖中、编译选项后面、清理项等),但是这样很麻烦,因为我们可以使用一个变量去定义所有的文件名,而在使用的时候我们只需要给出变量名即可,这样就不需要在多处重复写入文件名。

# 目标文件
edit : main.o add.o sub.o multiply.o divide.o
        cc -o edit main.o add.o sub.o multiply.o divide.o

# 规则定义
main.o : main.c main.h
        cc -c main.c

add.o : add.c main.h
        cc -c add.c

sub.o : sub.c main.h
        cc -c sub.c

multiply.o : multiply.c main.h
        cc -c multiply.c

divide.o : divide.c main.h
        cc -c divide.c

# 清理命令
clean :
        rm -f edit main.o add.o sub.o multiply.o divide.o

2.2 定义变量时的Makefile

如下,我们使用变量objects 包含了所有文件名,而在使用的时候我们直接给出这个变量名即可替代那些文件名,不需要重复填写,非常方便。

# 定义变量
objects = main.o add.o sub.o multiply.o divide.o

# 目标文件
edit : $(objects)
        cc -o edit $(objects)

# 规则定义
main.o : main.c main.h
        cc -c main.c

add.o : add.c main.h
        cc -c add.c

sub.o : sub.c main.h
        cc -c sub.c

multiply.o : multiply.c main.h
        cc -c multiply.c

divide.o : divide.c main.h
        cc -c divide.c

# 清理命令
clean :
        rm -f edit $(objects)
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MrWang.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值