title: How to define rules in the Makefile to compile only that *.cpp files which was modified (and their dependencies), not all *.cpp files
body: <p>Lets say I have files:</p>
  
  <p>Libs:</p>
  
  <ul>
  <li>one.cpp, one.h</li>
  <li>two.cpp, two.h</li>
  <li>three.cpp, three.h</li>
  </ul>
  
  <p>Program:</p>
  
  <ul>
  <li>program.cpp</li>
  </ul>
  
  <p>Is there way, to create Makefile which will compile only that *.cpp which were modified from last compilation?</p>
  
  <p>Currently I have something like that:</p>
  
  <pre><code>SRCS = one.cpp two.cpp three.cpp
  OBJS = $(SRCS:.cpp=.o)
  
  all: $(OBJS) program
  
  .cpp.o:
      g++ -Wall -c $&lt;
  
  program:
      g++ -Wall $(OBJS) program.cpp -o program
  
  clean:
      rm -f $(OBJS) program
  </code></pre>
  
  <p>I works fine, but when I compile my program and then change two.cpp or two.h I need to run "make clean" first, because when I secondly run "make" I get:</p>
  
  <pre><code>Nothing to be done for 'all'.
  </code></pre>
  
  <p>I would like to change my Makefile in that way, it would recognize my changes and recompile that file and its dependencies (if one.cpp uses code from two.cpp which was modified, both files should be recompiled).</p>
  
  <p>So if I modify two.cpp, make should do:</p>
  
  <pre><code>g++ -Wall -c two.cpp
  g++ -Wall $(OBJS) program.cpp -o program
  </code></pre>
  
  <p>But if one.cpp uses code from two.cpp which was modified, make shold do:</p>
  
  <pre><code>g++ -Wall -c one.cpp
  g++ -Wall -c two.cpp
  g++ -Wall $(OBJS) program.cpp -o program
  </code></pre>