views:

36

answers:

1

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>
+1  A: 

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...>
Beta
Generally, as your answer already makes clear: the requirement of *a directory as a dependency* is not well-specified. Sometimes (but very rarely) you want to rebuild whenever the directory itself has an update modification time, sometimes when any existing file in the directory changes, sometimes when any file of a certain type changes, sometimes you also want to rebuild when files of a certain type are added, deleted, or renamed, sometimes you want to recursively include the contents of subdirectories.rather expensive.
reinierpost