title: How do I ensure my makefile detects changes in my header and cpp files?
body: <p>Currently, whenever I do <code>make</code> my makefile tells me </p>
  
  <blockquote>
    <p>make: `some/obj/file.o' is up to date.</p>
  </blockquote>
  
  <p>regardless of whether I've edited any of the files involved in generating that object file. How do I make it detect changes? Here is a simple makefile that reproduces the problem:</p>
  
  <pre><code>SHELL := /bin/bash
  src := src
  sources := $(shell find $(srcDir) -name "*.cpp")                                
  objects := $(sources:%.cpp=%.o)                                                 
  
  -include $(sources:%.cpp=%.d)                                                   
  
  all: prog                                                                       
  
  prog:   $(objects)                                                              
      g++ $(objects) -o /a.out                                                    
  
  %.o: %.cpp                                                                      
      $(CXX) $(CXXFLAGS) -MMD -MP -c $&lt; -I $(srcDir) -o $@                        
  
  clean:                                                                          
      find $(srcDir) -type f -iname "*.o" -delete                                 
      find $(srcDir) -type f -iname "*.d" -delete 
  </code></pre>
  
  <p>currently I have to run <code>make clean</code> everytime to get it to recompile, which obviously isn't ideal!</p>
  
  <p><strong>EDIT: Here is my attempt based on Chnossos's answer:</strong></p>
  
  <pre><code>EXE := a.out                                                                    
  SRCDIR := src                                                                   
  SRC := $(shell find $(srcDir) -name "*.cpp")                                    
  DIR := .obj                                                                     
  OBJ := $(SRC:%.cpp=$(DIR)/%.o)                                                  
  DEP := $(OBJ:.o=.d)                                                             
  
  CXXFLAGS += -std=c++11                                                          
  CXXFLAGS += -I /home/arman/lib/eigen-eigen-6b38706d90a9                  
  CXXFLAGS += -I /home/arman/lib/boost_1_55_0                              
  CXXFLAGS += -I /home/arman/lib/lodepng/                                  
  CXXFLAGS += -L /home/arman/lib/boost_1_55_0/stage/lib                    
  CPPFLAGS += -MMD -MP                                                            
  
  .PHONY: all clean                                                               
  
  -include $(DEP)                                                                 
  
  all: $(EXE)                                                                     
  
  $(EXE):   $(OBJ)                                                                
          $(CXX) $(OBJ) -o $@                                                     
  
  $(DIR)/%.o: %.cpp                                                               
          $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $&lt; -I $(SRCDIR)                 
  
  clean:                                                                          
          $(RM) -f $(DIR)                                                         
  </code></pre>
  
  <p>I am now getting the following error:</p>
  
  <blockquote>
    <p>src/core/file1.cpp:839:1: fatal error: opening dependency file .obj/./src/core/file1.d: No such file or directory</p>
  </blockquote>
  
  <p>Note that I have the following directory structure:</p>
  
  <p>/prog/makefile -> The makefile
  /prog/dir1/    -> some cpp/hpp files
  /prog/dir2/    -> more cpp/hpp files
  /prog/         ->there are some cpp/hpp files here too </p>
  
  <p>I have many folders (more than just <code>dir1</code> and <code>dir2</code>) so I'd like to not have to specify them each time.</p>