title: How to make Makefile recompile when a header file is changed?
body: <p>I have written a Makefile to compile an openCV program on OSX (more general in Unix systems).</p>
  
  <p>The code has a header named <code>constants.hpp</code> where some constants are defined.</p>
  
  <p>I would like to make the Makefile recompile the program when this header file changes because the values of the constants in it change the program behavior.</p>
  
  <p>My Makefile is the following</p>
  
  <pre><code>CPP = g++
  CPPFLAGS = -std=c++11
  
  all: main.o
  
  main.o: main.cpp
      $(CPP) $^ $(CPPFLAGS) -o $@
  </code></pre>
  
  <p>Searching around I tried to define after <code>CPPFLAGS</code> the value:</p>
  
  <pre><code>DEPS = constants.hpp 
  </code></pre>
  
  <p>and then since <code>main.o</code> depends on it
  adding the dependency as follows:</p>
  
  <pre><code>main.o: main.cpp $(DEPS)
          $(CPP) $^ $(CPPFLAGS) -o $@
  </code></pre>
  
  <p>but the error I get is <code>clang: error: cannot specify -o when generating multiple output files</code>.</p>
  
  <p>I tried also <a href="https://stackoverflow.com/questions/2394609/makefile-header-dependencies">this answer</a> and tried to use th e <code>M</code> <code>MM</code> flags but I am missing something.</p>
  
  <p>How to make Makefile recompile when a header file is changed?</p>
  
  <p>EDIT: Following the comments to DevSolar I had to modify completely the question and he asked also the source code. Since I found useless to copy all the source code here I simplified it with a simple hello world program.</p>
  
  <p>The following is the <code>main.cpp</code>:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include"constants.hpp"
  
  int main()
  {
  
    std::cout&lt;&lt;"Hello world, the value is: " &lt;&lt;  myValue &lt;&lt;"\n";
    return 0;
  }
  </code></pre>
  
  <p>and the following the <code>constants.hpp</code>:</p>
  
  <pre><code>static const int myValue = 10;
  </code></pre>