How do I check dependencies when invoking a sub-make to build when there are changes?
-
05-09-2019 - |
Question
If I have a makefile that calls another makefile, how to I get the master makefile to correctly check if the dependencies of the subordinate makefile have changed?
For example, if I have the rule
server:
@cd $(SERVERDIR) && $(MAKE)
That invokes make in the subdirectory in which I build an executable "server". However, if I change one of the files that make up server, the parent make doesn't see the changes and refuses to rebuild server - "make: `server' is up to date."
How can I get the master makefile to correctly detect when there's a change in one of the dependent files (something like $(SERVERDIR)/server.c, for example?
Solution
It looks like you want to use a phony target
.PHONY: server
server:
@cd $(SERVERDIR) && $(MAKE)
There's a detailed description of the Phony target here, but the short description is you're telling the makefile that there will never be a file that corresponds with this server target, and therefore it won't consider server up to date if there is a file named server in the directory.
OTHER TIPS
Your target name matches the name of one of the files or directories in your main Makefile
directory.
Assuming you need to build everything in a subdirectory called server
, this rule:
server:
$(MAKE) -C server
will not work, as the target server
is a directory, has no source files and doesn't need to be built then.
This one:
srv:
$(MAKE) -C server
will work, as long as there is no file or directory called srv
.
You don't:
But yes, if you have no choice, e.g. because you don't control the sub-makefile, a .PHONY
target is what you are looking for.