views:

361

answers:

3

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?

+1  A: 

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.

Quassnoi
+5  A: 

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.

mbyrne215
This would normally be a good solution, but I don't want to *always* rebuild the target, I want it to behave like a normal non-recursive target and only build when something has changed.
Zxaos
That is the responsibility of your sub-makefile.
Randy Proctor
What Randy said. Since your server target doesn't have dependencies, it doesn't know "something has changed." Otherwise, what's the point of having the submakefile?
mbyrne215
Right, makes sense. Thanks!
Zxaos
+1  A: 

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.

Remy Blank