title: Use of 'Empty Target' in makefile
body: <p>Lately I've been trying to get into the concepts of GNU Make. While reading, I came accross a topic called <strong>Empty Target</strong>. From <a href="http://www.gnu.org/software/make/manual/make.html#Empty-Targets" rel="nofollow">GNU Make manual</a> what I got to know is </p>
  
  <blockquote>
    <p>The empty target is a variant of the phony target; it is used to hold
    recipes for an action that you request explicitly from time to time.
    Unlike a phony target, this target file can really exist; but the
    file's contents do not matter, and usually are empty. </p>
    
    <p>The purpose of the empty target file is to record, with its
    last-modification time, when the rule's recipe was last executed. It
    does so because one of the commands in the recipe is a touch command
    to update the target file. </p>
  </blockquote>
  
  <p>From <a href="http://books.google.co.in/books?id=rL4GthWj9kcC&amp;lpg=PP1&amp;dq=managing%20projects%20with%20gnu%20make&amp;pg=PT117#v=snippet&amp;q=empty%20targets&amp;f=false" rel="nofollow">another source</a>, I read something like this</p>
  
  <blockquote>
    <p>Empty targets are similar to phony targets in that the target itself
    is used as a device to leverage the capabilities of make. Phony
    targets are always out of date, so they always execute and they always
    cause their dependent (the target associated with the prerequisite) to
    be remade. But suppose we have some command, with no output file, that
    needs to be performed only occasionally and we don’t want our
    dependents updated? For this, we can make a rule whose target is an
    empty file (sometimes referred to as a cookie):</p>
  
  <pre><code>prog: size prog.o
  $(CC) $(LDFLAGS) -o $@ $^
  
  size: prog.o
  size $^
  touch size
  </code></pre>
    
    <p>Notice that the size rule uses touch to create an empty file named
    size after it completes. This empty file is used for its timestamp so
    that make will execute the size rule only when prog.o has been
    updated. Furthermore, the size prerequisite to prog will not force an
    update of prog unless its object file is also newer.</p>
  </blockquote>
  
  <p>From reading them what I  understood is</p>
  
  <ul>
  <li>Empty target is used to maintain the timestamp</li>
  <li>The content of empty target is of no interest, only the update time
  is.</li>
  </ul>
  
  <p>For doing some hands-on, I have written two sample makefiles</p>
  
  <h3>makefile 1</h3>
  
  <p>with empty target use</p>
  
  <pre><code>prog: size prog.o
      gcc -o $@ prog.o
  
  size: prog.o
      size $&lt;
      touch size
  
  prog.o: prog.c
      gcc -c $&lt;
  </code></pre>
  
  <p><em>output</em></p>
  
  <pre><code>[sourav@localhost Practice]# make
  gcc -c prog.c
  size prog.o
     text    data     bss     dec     hex filename
       58       0       0      58      3a prog.o
  touch size
  gcc -o prog prog.o
  [sourav@localhost Practice]# make
  make: `prog' is up to date.
  [sourav@localhost Practice]# touch prog.c //suppose some real change in the file
  [sourav@localhost Practice]# make
  gcc -c prog.c
  size prog.o
     text    data     bss     dec     hex filename
       58       0       0      58      3a prog.o
  touch size
  gcc -o prog prog.o
  [sourav@localhost Practice]#
  </code></pre>
  
  <h3>makefile 2</h3>
  
  <p>without empty target use</p>
  
  <pre><code>prog: prog.o
      size $^
      gcc -o $@ prog.o
  
  prog.o: prog.c
      gcc -c $&lt;
  </code></pre>
  
  <p><em>output</em></p>
  
  <pre><code>[sourav@localhost Practice]# make
  gcc -c prog.c
  size prog.o
     text    data     bss     dec     hex filename
       58       0       0      58      3a prog.o
  gcc -o prog prog.o
  [sourav@localhost Practice]# make
  make: `prog' is up to date.
  [sourav@localhost Practice]# touch prog.c 
  [sourav@localhost Practice]# make
  gcc -c prog.c
  size prog.o
     text    data     bss     dec     hex filename
       58       0       0      58      3a prog.o
  gcc -o prog prog.o
  [sourav@localhost Practice]#
  </code></pre>
  
  <p>In above two different makefiles , the output for the similar condition is same. So, I'm not clear with the particular <strong><em>scope of use</em></strong> for empty targets. If they are used only to hold the timestamp, then we can achieve the same without using them . [Or is related with automatic variable <code>$?</code> about which I'm yet to read in detail]. Can anyone resolve this confusion in some other wordings, possibly with some example (if I'm not asking too much)?</p>
  
  <p>Thanks and Cheers!!</p>