出现“make:Nothing to be do for 'target'”的错误

发布于 2024-11-25 11:25:02 字数 630 浏览 0 评论 0原文

让我用一个例子来说明一下。

mkdir test
cd test
touch target
make target

这将导致: make: Nothing to be do for 'target'.

所以 ma​​ke 告诉我没有什么可做的。这是因为 ma​​ke 没有找到 make target 的规则,而是因为 target 已经存在 ma​​ke 告诉我无事可做。

现在,我不想那样。我希望 ma​​ke 在找不到 target 的规则时给出错误,即使 target 已经存在。

我已尝试以下操作:

echo '.DEFAULT:
        echo make: *** No rule to make target `$@'.  Stop.
        false'> Makefile

但这不会在创建多个目标时停止 ma​​ke

Let me illustrate it with an example.

mkdir test
cd test
touch target
make target

This will result in: make: Nothing to be done for 'target'.

So make tells me there is nothing to do. This is because make did not find a rule to make target, but because the target already exists make tells me there is nothing to do.

Now, I don't want that. I want make to give me an error when it cannot find a rule for target, even though the target already exists.

I have tried the following:

echo '.DEFAULT:
        echo make: *** No rule to make target `$@'.  Stop.
        false'> Makefile

But this does not stop the make when making multiple targets.

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

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

发布评论

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

评论(3

明月夜 2024-12-02 11:25:02

问题是, make 假设目标名称也是一个将由给定命令构建的文件。

但有时这不是真的(例如想想“干净”)。
要告诉 make 某些目标不构建此文件,您需要将它们设置为“虚假”。将以下行放入您的 Makefile 中:

.PHONY: target

The problem is, that make assumes the target name is also a file which will be build by the given commands.

But sometimes this is not true (e.g. think of "clean").
To tell make that some targets don't build this file, you need to make them "phony". Put the following line into your Makefile:

.PHONY: target
以可爱出名 2024-12-02 11:25:02

如果你仔细想想,你最终会陷入先有鸡还是先有蛋的情况(或无限倒退)。假设您设法制定了一条规则,规定“您必须有一条规则来创建目标 目标 才合法”,那么您将拥有对于某些其他文件X,表示“目标依赖于X”的规则。是这样写的:

target: X
        command to build target from X

但是随后您会回到起点:您还需要一个规则来创建X。你怎么能这么做呢?您可能有一个不依赖于任何内容的规则,并在需要时神奇地创建文件X

X:
        command to build X from nothing

如果没有这样的规则,您就会陷入无限倒退。

因此,make 旨在确保文件存在且是最新的。如果文件存在并且没有规则(隐式或显式)来指定它的制作方式,则该文件是最新的。你想要做的事情是不可能的。

If you think about it, you would end up with a chicken-and-egg situation (or infinite regress). Suppose you managed to have a rule that said 'You must have a rule to create target before it is legitimate', then you'd have a rule that says 'target depends on X' for some other file X. That's written:

target: X
        command to build target from X

But then you'd be back to the starting point: you'd also want a rule to create X. How can you do that? You might have a rule that depends on nothing and magically creates the file X when it is needed:

X:
        command to build X from nothing

Without a rule like that, you have an infinite regress.

So, make is designed to ensure that files exist and are up to date. If a file exists and there are no rules - implicit or explicit - to specify how it is made, then the file is up to date. What you are seeking to do is not possible.

凝望流年 2024-12-02 11:25:02

实际上,这听起来像是一个完全合理的(如果被误导了;-))请求。不过,您必须使用带有空配方的规则显式列出每个源文件。

给你:

Makefile: ;

.PHONY: dodgy
dodgy%: dodgy; $(error You must provide a rule for $*)
%: dodgy% ;

布丁的证明:

$ rm aa
$ make aa
Makefile:4: *** You must provide a rule for aa.  Stop.
$ touch aa
$ make aa
Makefile:4: *** You must provide a rule for aa.  Stop.

请注意,Makefile: ; 行是必需的。毕竟,make 尝试做的第一件事就是重建 Makefile。

还要注意,包罗万象的模式规则是非终结的。这可能会对性能造成巨大影响。正如手册中所说的匹配任何规则“它们非常有用,但 make 可能需要很多时间来考虑它们。”

Actually this sounds like a perfectly reasonable (if misguided ;-)) request. You will have to explicitly list every source file with a rule with an empty recipe though.

Here you go:

Makefile: ;

.PHONY: dodgy
dodgy%: dodgy; $(error You must provide a rule for $*)
%: dodgy% ;

The proof of the pudding:

$ rm aa
$ make aa
Makefile:4: *** You must provide a rule for aa.  Stop.
$ touch aa
$ make aa
Makefile:4: *** You must provide a rule for aa.  Stop.

Note that the line Makefile: ; is necessary. After all, the first thing make tries to do is rebuild the Makefile.

Note also that the catch-all pattern rule is non-terminal. This can be a massive performance hit. As the manual says about match anything rules "They are very useful, but it can take a lot of time for make to think about them."

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