title: About the GNU make dependency files *.d
body: <p>In the makefile of a program, one has to write rules that define the dependencies of each object file. Consider the object file <code>fileA.o</code>. It is obvious that this object file depends on the source file <code>fileA.c</code>. But it will also depend on all the header files that this source file includes. So the following rule should be added to the makefile:</p>
  
  <pre><code>    # This rule states that fileA.o depends on fileA.c (obviously), but also
      # on the header files fileA.h, fileB.h and fileC.h
      fileA.o: fileA.c fileA.h fileB.h fileC.h
  </code></pre>
  
  <p>Note that the rule has no recipe. One could add a recipe to it, but it is strictly speaking not necessary, because GNU make can rely on an implicit rule (with recipe) to compile a <code>*.c</code> file into a <code>*.o</code> file.</p>
  
  <p>Anyway, writing such rules manually is a hellish task. Just imagine the work to keep the makefile rules in sync with the #include statements from the source code.</p>
  
  <p>The GNU make manual describes in chapter 4.14 "Generating Prerequisites Automatically" a methodology to automate this procedure. The procedure starts with the generation of a <code>*.d</code> file for each source file. I quote:</p>
  
  <blockquote>
    <p>For each source file <strong>name.c</strong> there is a makefile <strong>name.d</strong> which lists what files the object file <strong>name.o</strong> depends on.</p>
  </blockquote>
  
  <p>The manual proceeds:</p>
  
  <blockquote>
    <p>Here is the pattern rule to generate a file of prerequisites (i.e, a makefile) called <strong>name.d</strong> from a C source file called <strong>name.c</strong> :</p>
  </blockquote>
  
  <pre><code>    %.d: %.c
          @set -e; rm -f $@; \
           $(CC) -M $(CPPFLAGS) $&lt; &gt; $@.$$$$; \
           sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' &lt; $@.$$$$ &gt; $@; \
           rm -f $@.$$$$
  </code></pre>
  
  <p>Sadly, the manual does not explain in detail how this rule actually works. Yes, it gives the desired <strong>name.d</strong> file, but why? The rule is very obfuscated..</p>
  
  <p>When looking at this rule, I get the feeling that its recipe will only run smoothly on Linux. Am I right? Is there a way to make this recipe run correctly on Windows as well?</p>
  
  <p>Any help is greatly appreciated :-)</p>