Makefile 简明教程
Makefile - Other Features
在本章中,我们将研究 Makefile 的其他各种特性。
In this chapter, we shall look into various other features of Makefile.
Recursive Use of Make
递归使用 make 意味着在 makefile 中使用 make 作为命令。当您想要为组成一个较大系统的各个子系统编写单独的 makefile 时,此技术非常有用。例如,假设您有一个名为 subdir
的子目录,它有自己的 makefile,并且您希望包含目录的 makefile 在子目录上运行 make 。您可以通过编写以下代码来实现:
Recursive use of make means using make as a command in a makefile. This technique is useful when you want separate makefiles for various subsystems that compose a larger system. For example, suppose you have a subdirectory named `subdir' which has its own makefile, and you would like the containing directory’s makefile to run make on the subdirectory. You can do it by writing the below code −
subsystem:
cd subdir && $(MAKE)
or, equivalently:
subsystem:
$(MAKE) -C subdir
您可以通过复制此示例来编写递归 make 命令。但是,您需要了解它们如何工作以及原因,以及子 make 如何与顶级 make 发生关联。
You can write recursive make commands just by copying this example. However, you need to know about how they work and why, and how the sub-make relates to the top-level make.
Communicating Variables to a Sub-Make
通过明确请求,可以将顶级 make 的变量值通过环境传递给子 make。这些变量在子 make 中定义为默认值。除非使用 -e
开关,否则不能覆盖子 make makefile 中指定的 makefile。
Variable values of the top-level make can be passed to the sub-make through the environment by explicit request. These variables are defined in the sub-make as defaults. You cannot override what is specified in the makefile used by the sub-make makefile unless you use the `-e' switch.
要传递或导出变量, make 将变量及其值添加到运行每个命令的环境中。子 Makefile 会使用该环境来初始化其变量值的表。
To pass down, or export, a variable, make adds the variable and its value to the environment for running each command. The sub-make, in turn, uses the environment to initialize its table of variable values.
特殊变量 SHELL 和 MAKEFLAGS 始终会导出(除非取消导出)。如果将 MAKEFILES 设置为任何值,则会导出 MAKEFILES。
The special variables SHELL and MAKEFLAGS are always exported (unless you unexport them). MAKEFILES is exported if you set it to anything.
如果要将特定变量导出到子 Makefile,请使用 export 指令,如下所示:
If you want to export specific variables to a sub-make, use the export directive, as shown below −
export variable ...
如果要阻止导出某个变量,请使用 unexport 指令,如下所示
If you want to prevent a variable from being exported, use the unexport directive, as shown below −
unexport variable ...
The Variable MAKEFILES
如果定义环境变量 MAKEFILES, make 会将它的值视为在读取其他 Makefile 之前要读取的附加 Makefile 的名称列表(由空格分隔)。这与 include 指令非常相似:会搜索各种目录查找这些文件。
If the environment variable MAKEFILES is defined, make considers its value as a list of names (separated by white space) of additional makefiles to be read before the others. This works much like the include directive: various directories are searched for those files.
使用 MAKEFILES 的主要目的是在 make 的递归调用之间进行通信。
The main use of MAKEFILES is in communication between recursive invocations of the make.
Including Header file from Different Directories
如果您将头文件放在不同的目录中,并且在不同的目录中运行 make ,则需要提供头文件的路径。这可以通过 Makefile 中的 -I 选项完成。假设 /home/tutorialspoint/header 文件夹中有 functions.h 文件,其余文件都位于 /home/tutorialspoint/src/ 文件夹中,那么 Makefile 将被写成如下形式:
If you have put the header files in different directories and you are running make in a different directory, then it is required to provide the path of header files. This can be done using -I option in makefile. Assuming that functions.h file is available in /home/tutorialspoint/header folder and rest of the files are available in /home/tutorialspoint/src/ folder, then the makefile would be written as follows −
INCLUDES = -I "/home/tutorialspoint/header"
CC = gcc
LIBS = -lm
CFLAGS = -g -Wall
OBJ = main.o factorial.o hello.o
hello: ${OBJ}
${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}
.cpp.o:
${CC} ${CFLAGS} ${INCLUDES} -c $<
Appending More Text to Variables
通常,将更多文本添加到已定义变量的值很有用。您可以使用包含 +=
的行来完成此操作,如下所示:
Often it is useful to add more text to the value of a variable already defined. You do this with a line containing `+=', as shown −
objects += another.o
它采用变量 objects 的值,并在其后添加文本 another.o
,前面有一个空格,如下所示。
It takes the value of the variable objects, and adds the text `another.o' to it, preceded by a single space as shown below.
objects = main.o hello.o factorial.o
objects += another.o
上述代码将 objects 设置为 main.o hello.o factorial.o another.o
。
The above code sets objects to `main.o hello.o factorial.o another.o'.
使用 +=
与以下类似:
Using `+=' is similar to:
objects = main.o hello.o factorial.o
objects := $(objects) another.o
Continuation Line in Makefile
如果您不喜欢 Makefile 中过长的行,则可以使用反斜杠 \"
来断行,如下所示:
If you do not like too big lines in your Makefile, then you can break your line using a back-slash "\" as shown below −
OBJ = main.o factorial.o \
hello.o
is equivalent to
OBJ = main.o factorial.o hello.o
Running Makefile from Command Prompt
如果您已准备好名为“Makefile”的 Makefile,则只需在命令提示符处编写 make,它便会运行 Makefile。但是,如果您给 Makefile 指定了任何其他名称,则使用以下命令:
If you have prepared the Makefile with name "Makefile", then simply write make at command prompt and it will run the Makefile file. But if you have given any other name to the Makefile, then use the following command −
make -f your-makefile-name