Directory as a dependency in make rule

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

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

Now I want that whenever I make any changes to the src directory, that is, to any of ac or ah, a specific rule in my Makefile is called when the make command is issued. Sort of

 Makefile . . . build: src <commands> clean: <commands> 
+4
source share
2 answers

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 commands-to-make-the-file when the file older than the directory , which might not be what you want.

You need to consider scenarios in which the directory timestamp is updated. This happens whenever files are added or removed from the directory, but this does not happen when an existing file is modified.

So, some unrelated actions that update the directory will make the file obsolete, perhaps unnecessarily, which will cause commands to recreate it.

+3
source

It is possible that a directory exists as a dependency, in the sense that if it does not exist, it will be rebuilt. It is even possible to organize a rule that will be executed when something in the directory changes, but it is difficult. And in this case, it almost certainly overwhelms.

If your intention is ordinary, the usual method will be sufficient:

 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...> 
+2
source

All Articles