从父 Shell 脚本创建变量

Make Variables from Parent Shell Script

在我正在阅读的 makefile 中,有许多对变量 projdir 的引用,在调用 make 的 shell 脚本中定义和导出; makefile 中有很多 $(projdir) 的实例。

http://www.gnu.org/software/make/manual/make.html#Reference的make手册来看,只有两种类型的变量:递归扩展变量和简单扩展变量。 projdir 似乎都不是。

问题 1:既然 makefile 可以工作,makefile 一定可以访问父 shell 环境中定义的 shell 变量。为什么没有记录(或者我没有找到正确的文档)?

问题 2:与问题 1 无关,我在第 6.2 节中看到行 all:;echo $(foo)。为什么这里需要分号;

来自make manual:

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value.

关于你的第二个问题:

Why is the semicolon ; needed here?

如果没有分号,: 的右侧将指定依赖项列表。分号终止该(空)列表。

问题一、环境变量自动变成make变量。这在 section 6.10:

中有解释

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value.

问题 2:在规则名称和可选的先决条件之后放置一个分号可以让您在同一行启动配方。这在 section 4.2:

中有解释

The first recipe line may appear on the line after the prerequisites, with a tab character, or may appear on the same line, with a semicolon.

为了简洁起见,他们在 6.2 的示例中使用了这种语法,因此他们可以在句子中内联显示整个规则;我认为它在实践中很少被使用。

变量有两个一个flavor和一个origin

扩展类型是 flavor

make 包含一个 flavor function 可用于查找相关变量的类型。它将 return undefinedrecursivesimple 之一。

变量也有一个 origin,这是它们的值的来源。您在这里看到的是这些可能的来源之一。

origin function会告诉你变量的来源。

有七种可能的来源:undefineddefaultenvironmentenvironment overridefilecommand lineoverrideautomatic.

因此您的 projdir 变量具有 simpleflavorenvironmentorigin