目录作为 make 规则中的依赖项
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然可以将目录作为依赖项,但有一些事情需要注意。考虑一下:
这将按照您的想法进行。但是,只要
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:
This will do what you think. However, it executes the
commands-to-make-the-file
wheneverfile
is older thandirectory
, 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.
可能有一个目录作为依赖项,从某种意义上说,如果它不存在,它将被重建。甚至可以安排一个规则,当目录中的任何内容发生更改时执行,但这很棘手。 在这种情况下,这几乎肯定是矫枉过正。
如果您的意图很普通,那么通常的方法就足够了:
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: