门户网站建设方案招标文件,网站建设会考什么,正规的扬中网站建设,网页设计教程书籍推荐Variables Make Makefiles Simpler
一、本节概要
Variables Make Makefiles Simpler #xff08;变量使Makefile更简单#xff09;#xff0c;以下是官方给出的原文#xff0c;接下来会对本节内容进行拆解#xff0c;并给出详细示例代码。
In our example, we had to l…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.oSuch 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.occ -o edit main.o add.o sub.o multiply.o divide.o# 规则定义
main.o : main.c main.hcc -c main.cadd.o : add.c main.hcc -c add.csub.o : sub.c main.hcc -c sub.cmultiply.o : multiply.c main.hcc -c multiply.cdivide.o : divide.c main.hcc -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.hcc -c main.cadd.o : add.c main.hcc -c add.csub.o : sub.c main.hcc -c sub.cmultiply.o : multiply.c main.hcc -c multiply.cdivide.o : divide.c main.hcc -c divide.c# 清理命令
clean :rm -f edit $(objects)