4

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?

Zxaos
  • 7,791
  • 12
  • 47
  • 61

3 Answers3

7

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
  • 2,304
  • 4
  • 21
  • 16
  • 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 Feb 17 '09 at 15:48
  • 2
    That is the responsibility of your sub-makefile. – Randy Proctor Feb 17 '09 at 15:49
  • 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 Feb 17 '09 at 15:55
1

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
  • 413,100
  • 91
  • 616
  • 614
1

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
  • 4,236
  • 2
  • 23
  • 24