views:

905

answers:

3

I am learning makefiles and I know how to create a simple makefile. I am moving on to nested makefiles. Here is my directory structure

/src
...makefile
...main.cpp
...foo
......makefile
......foo.cpp
......foo.h

When root makefile is called, it calls the makefile in directory foo. Here are my questions

  1. Which makefile should I use to write code for linking all object files? If it is in the root makefile, do I need to specify all object file names there?
  2. Is this nested makefiles a best practice? Or is it good to have only one makefile which is at root?

Any help would be great!

+2  A: 

Recursive make is generally considered harmful.

If you really want to be able to type "make" in the root dir and have it build everything using only POSIX makefiles, it is the only way. In that case, link the subprojects in their own directory, and final stuff together in root.

If you're willing to use gmake syntax, take a look at the Makefiles here: http://github.com/singpolyma/theveeb-ecosystem

singpolyma
You can use a POSIX (non-GNU) make to perform a non-recursive build by breaking it into two steps: 1) concatenate all make.inc files in all subdirs; 2) run the resulting makefile. But most people use GNU make anyway, which has the "include" directive.
j_random_hacker
Ah, that's true, I never though of the concatenation hack. I tend to use include. Normally I want to be very POSIX, but with makefiles sometimes that's a bit hairy because of the no include directive :)
singpolyma
A: 

There are more modern build systems like SCons That have an easier syntax than make and avoid many pitfalls of make. For example SCons scans the source files to determine dependencies on it's own, whereas for make one needs to specify dependencies manually. If you e.g. add a new #include statement to an implementation file make will not recompile that implementation file if the header changes (unless you add the new dependency). SCons will automatically detect the new dependency and recompile whatever necessary.

lothar