title: Using g++ with -MMD in makefile to automatically generate dependencies
body: <p>I know the following makefile will have the pre-processor automatically generate dependencies (in .d files) and include them in the makefile (because my course notes say so), so that they do not have to be automatically maintained. The <code>-MMD</code> flag is what's responsible for this. What I don't get is: At what point are the .d files generated? There isn't even any command where <code>${CXXFLAGS}</code> is used. Presumably, commands like <code>${CXX} ${CXXFLAGS} -c x.C -o x.o</code> will be automatically deduced by make for each of the object files, but if these are the commands that generate the .d files, wouldn't we have already passed the point where knowing the dependencies of x.o, y.o and z.o could've been relevant, if we only know them by executing the commands that generate these .o files? (Say there are .h files that the makefile would ignore if left to deduce the rules on its own or something.)</p>
  
  <pre><code>CXX = g++                     # compiler
  CXXFLAGS = -g -Wall -MMD      # compiler flags
  OBJECTS = x.o y.o z.o         # object files forming executable
  DEPENDS = ${OBJECTS:.o=.d}    # substitutes ".o" with ".d"
  EXEC = a.out                  # executable name
  
  ${EXEC} : ${OBJECTS}          # link step
      ${CXX} ${OBJECTS} -o ${EXEC}
  
  -include ${DEPENDS}           # copies files x.d, y.d, z.d (if they exist)
  </code></pre>