目录作为 make 规则中的依赖项

发布于 2024-09-25 15:49:14 字数 336 浏览 1 评论 0原文

是否可以在 Makefile 规则中指定一个目录作为依赖项?实际上,我在一个目录和另一个包含所有源文件的目录中有一个 Makefile。

.
.
|_ Makefile
|_ src
    |_a.c
    |_a.h

现在我希望每当我在 src 目录中进行任何更改(即在 ac 或 ah 中)时,我的 Makefile 中的特定规则都会在发出 make 命令时被调用。像这样的东西

Makefile
.
.
.
build: src
    <commands>

clean:
    <commands>

Is it possible to specify a directory as a dependency in a Makefile rule? Actually I have a Makefile in a directory and another directory containing all the source files.

.
.
|_ Makefile
|_ src
    |_a.c
    |_a.h

Now I want that whenever I make any change in the src directory i.e. in either of a.c or a.h , a particular rule in my Makefile get called on issuing make command. Something like

Makefile
.
.
.
build: src
    <commands>

clean:
    <commands>

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

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

发布评论

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

评论(2

塔塔猫 2024-10-02 15:49:14

虽然可以将目录作为依赖项,但有一些事情需要注意。考虑一下:

directory:
        @mkdir -p directory

directory/file : directory
        commands-to-make-the-file

这将按照您的想法进行。但是,只要 file 早于 directory,它就会执行 commands-to-make-the-file,这可能不是您想要的。

您需要考虑 directory 的时间戳更新的场景。每当将文件添加到目录或从目录中删除文件时都会发生这种情况,但修改现有文件时不会发生这种情况。

因此,更新目录的一些不相关的操作将导致文件过时,也许是不必要的,这将触发重新创建文件的命令。

While it is possible to have a directory as a dependency, there are a few things to be aware of. Consider this:

directory:
        @mkdir -p directory

directory/file : directory
        commands-to-make-the-file

This will do what you think. However, it executes the commands-to-make-the-file whenever file is older than directory, which may not be what you want.

You need to consider the scenarios where the timestamp of directory gets updated. That happens whenever files are added into or removed from the directory but it doesn't happen when an existing file is modified.

So, some unrelated action that updates the directory will cause the file to become out of date, perhaps needlessly so, which will trigger the commands to re-make it.

土豪我们做朋友吧 2024-10-02 15:49:14

可能有一个目录作为依赖项,从某种意义上说,如果它不存在,它将被重建。甚至可以安排一个规则,当目录中的任何内容发生更改时执行,但这很棘手。 在这种情况下,这几乎肯定是矫枉过正。

如果您的意图很普通,那么通常的方法就足够了:

OBJ_FILES = foo.o bar.o baz.o
# There are ways to be more succinct, but for now we'll keep it simple.

build: $(OBJ_FILES)
    <commands...>

%.o: src/%.c src/%.h
    <commands for building something.o>

clean: # This should not require prerequisites
    <commands...>

It is possible to have a directory as a dependency, in the sense that if it does not exist it will be rebuilt. It's even possible to arrange a rule that will execute when anything in the directory changes, but it's tricky. And in this case it's almost certainly overkill.

If your intent is ordinary, the usual method will suffice:

OBJ_FILES = foo.o bar.o baz.o
# There are ways to be more succinct, but for now we'll keep it simple.

build: $(OBJ_FILES)
    <commands...>

%.o: src/%.c src/%.h
    <commands for building something.o>

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