使所有规则取决于 Makefile 本身

发布于 2024-09-26 11:15:32 字数 162 浏览 4 评论 0原文

当我更改 Makefile 时,它​​的规则可能已更改,因此应该重新评估它们,但 make 似乎并不这么认为。

有没有什么办法可以说,在 Makefile 中,它的所有目标,无论是哪个,都依赖于 Makefile 本身? (不管它的名字是什么。)

我正在使用 GNU make。

When I change a Makefile, its rules may have changed, so they should be reevaluated, but make doesn't seem to think so.

Is there any way to say, in a Makefile, that all of its targets, no matter which, depend on the Makefile itself?
(Regardless of its name.)

I'm using GNU make.

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

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

发布评论

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

评论(3

强辩 2024-10-03 11:15:32

这看起来像是 Make 应该能够做到的一件更简单、有用、合乎逻辑的事情,但事实并非如此。

这是一个解决方法。如果 clean 规则设置正确,只要 makefile 被更改,Make 就可以使用空的 dummy 文件作为标记来执行它。

-include dummy

dummy: Makefile
    @touch $@
    @$(MAKE) -s clean

这适用于大多数目标,即是实际文件并被 clean 删除的目标,以及依赖于它们的任何目标。副作用目标和一些PHONY目标会漏网。

This looks like one more simple, useful, logical thing that Make should be able to do, but isn't.

Here is a workaround. If the clean rule is set up correctly, Make can execute it whenever the makefile has been altered, using an empty dummy file as a marker.

-include dummy

dummy: Makefile
    @touch $@
    @$(MAKE) -s clean

This will work for most targets, that is targets that are actual files and that are removed by clean, and any targets that depend on them. Side-effect targets and some PHONY targets will slip through the net.

护你周全 2024-10-03 11:15:32

自 GNU make 4.3 版本以来,现在可以使用这两个 特殊变量

  1. .EXTRA_PREREQS
    • 为每个目标添加新的先决条件
  2. MAKEFILE_LIST
    • 获取make文件的路径

让每个目标都依赖于当前的 make 文件:

将以下行放在文件顶部附近(在任何包含之前,因为它会影响 MAKEFILE_LIST):

.EXTRA_PREREQS:= $(abspath $(lastword $(MAKEFILE_LIST)))

让每个目标都依赖于当前的 make 文件以及包含的 make 文件

将以下行放在文件末尾:

    .EXTRA_PREREQS+=$(foreach mk, ${MAKEFILE_LIST},$(abspath ${mk}))

Since GNU make version 4.3 it is now possible with the use of those two special variable:

  1. .EXTRA_PREREQS
    • To add new prerequisite to every target
  2. MAKEFILE_LIST
    • To get the path of the make file

To have every target depend on the current make file:

Put near the top of the file (before any include since it would affect the MAKEFILE_LIST) the following line:

.EXTRA_PREREQS:= $(abspath $(lastword $(MAKEFILE_LIST)))

To have every target depend on the current make file and also the make files which were included

Put the following line at the end of your file:

    .EXTRA_PREREQS+=$(foreach mk, ${MAKEFILE_LIST},$(abspath ${mk}))
維他命╮ 2024-10-03 11:15:32

我知道的唯一答案是将 makefile 显式添加到依赖项中。例如,

%.o: %.c makefile
        $(CC) $(CFLAGS) -c 
lt;

The only answer I know to this is to add makefile explicitly to the dependencies. For example,

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