简体   繁体   English

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

[英]Directory as a dependency in make rule

Is it possible to specify a directory as a dependency in a Makefile rule? 是否可以在Makefile规则中将目录指定为依赖项? Actually I have a Makefile in a directory and another directory containing all the source files. 实际上,我在一个目录中有一个Makefile,而另一个目录中包含所有源文件。

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

Now I want that whenever I make any change in the src directory ie in either of ac or ah , a particular rule in my Makefile get called on issuing make command. 现在,我希望每当我在src目录(即ac或ah)中进行任何更改时,在发出make命令时都会调用我的Makefile中的特定规则。 Something like 就像是

Makefile
.
.
.
build: src
    <commands>

clean:
    <commands>

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. 但是,只要file早于directory ,它就会执行“ commands-to-make-the-filecommands-to-make-the-file ,这可能不是您想要的。

You need to consider the scenarios where the timestamp of directory gets updated. 您需要考虑directory时间戳更新的情况。 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: 如果您的意图是普通的,则通常的方法就足够了:

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...>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM