makefile中处理组件的顺序

发布于 2025-01-02 11:12:49 字数 162 浏览 1 评论 0原文

在 makefile 中,依赖行的形式为 -

abc: x y z

所有三个组件 (x,y,z) 本身都是 makefile 中依赖行中的目标。

如果调用make abc,三个目标x、y、z将以什么顺序执行?

In a makefile, the dependency line is of the form -

abc: x y z

All three of the components (x,y,z) are themselves targets in dependency lines further down in the makefile.

If make abc is invoked, in what order will the three targets x,y,z be executed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄意 2025-01-09 11:12:49

默认情况下,执行顺序与先决条件列表中指定的顺序相同,除非这些先决条件之间定义了任何依赖关系。

abc: x y z

顺序是xy z

abc: x y z
y : z

顺序为xz y

但理想情况下,您应该设计 Makefile,使其不依赖于指定先决条件的顺序。也就是说,如果 y 应在 z 之后执行,则必须存在 y : z 依赖性。

请记住,GNU Make 可以并行执行一些配方,请参阅 Mat 的回答

By default, the order of execution is the same as specified in the prerequisites list, unless there are any dependencies defined between these prerequisites.

abc: x y z

The order is x y z.

abc: x y z
y : z

The order would be x z y.

But ideally, you should design your Makefiles so that it wouldn't rely on the order in which prerequisites are specified. That is, if y should be executed after z, there must be a y : z dependence.

And keep in mind that GNU Make can execute some recipes in parallel, see Mat's answer.

对你的占有欲 2025-01-09 11:12:49

您确实不应该依赖它们的执行顺序 - 在其他条件相同的情况下,这些先决条件的所有三个配方都可以并行运行。

唯一的硬性规则是在运行目标配方之前必须满足所有先决条件。

如果 xyz 之间没有依赖关系,并且没有并行执行,GNU make 会按照您指定的顺序运行它们,但这在文档中并没有得到保证。

You really shouldn't depend on the order in which they are executed - all else being equal, all three recipes for those prerequisites could run in parallel.

The only hard rule is that all prerequisites must be met before the target recipe is run.

If there are no dependencies between x, y and z, and no parallel execution, GNU make appears to run them in the order you specified them, but this is not guaranteed in the docs.

缱绻入梦 2025-01-09 11:12:49

make 的 POSIX 描述包含一个基本原理其中说:

大多数历史实现中的 make 实用程序按从左到右的顺序处理目标的先决条件,并且 makefile 格式需要这样做。它支持许多生成 yacc 程序的 makefile 中使用的标准习惯用法;例如:

foo: y.tab.o lex.o main.o
     $(CC) $(CFLAGS) -o $@ t.tab.o lex.o main.o

在此示例中,如果 make 选择任意顺序,则可能无法使用正确的 y.tab.h 生成 lex.o >。尽管可能有更好的方式来表达这种关系,但它在历史上被广泛使用。想要并行更新先决条件的实现应该需要对 make 或 makefile 格式的显式扩展来完成它,如前面所述。

我相信 $(CC) 行中的 t.tab.oy.tab.o 的拼写错误,但这就是基本原理实际上所说的。

因此,观察到的从左到右处理先决条件的行为在这里得到了验证,尽管它只在基本原理部分而不是在主要描述中。基本原理还提到了并行 make 等问题。

The POSIX description of make includes a rationale which says:

The make utilities in most historical implementations process the prerequisites of a target in left-to-right order, and the makefile format requires this. It supports the standard idiom used in many makefiles that produce yacc programs; for example:

foo: y.tab.o lex.o main.o
     $(CC) $(CFLAGS) -o $@ t.tab.o lex.o main.o

In this example, if make chose any arbitrary order, the lex.o might not be made with the correct y.tab.h. Although there may be better ways to express this relationship, it is widely used historically. Implementations that desire to update prerequisites in parallel should require an explicit extension to make or the makefile format to accomplish it, as described previously.

(I believe the t.tab.o in the $(CC) line is a typo for y.tab.o, but that is what the rationale actually says.)

Thus, the observed behaviour that pre-requisites are processed from left to right has validation here, though it is only in the Rationale section, not in the main description. The Rationale also mentions issues with parallel make etc.

时光是把杀猪刀 2025-01-09 11:12:49

GNU make 4.4 的新功能是,我们有 .WAIT 先决条件!

将其设置为依赖项,它将暂停其右侧的所有依赖项,直到左侧的所有依赖项完成。

all: please_run_me and_me .WAIT before_me

请注意,please_run_me & and_me 彼此之间没有顺序,但两者都会在 before_me 开始之前完成。

您可以在一行上抛出多个 .WAIT

还有另一种方法可以做到这一点 - .NOTPARALLEL,您可以阅读它 更改日志中手册


虽然它是在 2022 年 10 月 31 日发布的,某些发行版(例如:Ubuntu)可能尚未预装。

如果您想为此类设备安装 make,则必须从源代码构建。

New to GNU make 4.4, we have the .WAIT prerequisite!

Set it as a dependency, and it will pause all dependencies to the right of it until all the ones to the left are completed.

all: please_run_me and_me .WAIT before_me

Note that please_run_me & and_me are not ordered in relation to eachother, but both will finish before before_me starts.

You can throw multiple .WAITs on a line.

There was another way added to do this too - .NOTPARALLEL, you can read about it in the changelog or in the manual


Though it released on the 31st October 2022, some distributions (e.g.: Ubuntu) may not come with it preinstalled yet.

If you want to install make for such devices, you'll have to build from source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文