title: makefile automatic src file detection and dependency generation
body: <p>I have the following setup for my c++ project:</p>
  
  <p>in the src-folder, there are *.cpp and corresponding *.h files and
  in the obj-folders I want to have my .o-files.</p>
  
  <p>so far, compiling and linking is not the problem. But now I have too many .cpp and .h files to put them into the Makefile manually. So I decided to write this little instruction in the makefile:</p>
  
  <pre><code>collect_sources:
  @echo "collecting source files";
  @echo "SRCS = \\" &gt; sources;
  @for file in ./src/*.cpp; \
  do \
      echo "$$file \\" &gt;&gt; sources; \
  done
  </code></pre>
  
  <p>I also do</p>
  
  <pre><code>-include sources
  </code></pre>
  
  <p>at the beginning of the makefile</p>
  
  <p>The resulting file <code>sources</code> looks fine, although there is a backslash in the last line which I don't like. But afaik it should be harmful either.</p>
  
  <p>I now also need to automatically build up dependencies. In my last version in which I defined the *.cpp-files as <code>SRCS</code> directly in the Makefile, the following code was good:</p>
  
  <pre><code>$(SRCDIR)/%.cpp:
    @$(CXX) $(DEPFLAGS) -MT \
    "$(subst $(SRCDIR),$(OBJDIR),$(subst $(EXT_SRC),$(EXT_OBJ),$$file))" \
    $(addprefix ,$$file) &gt;&gt; $(DEP);
  clear_dependencies:
      echo "" &gt; $(DEP);
  
  depend: clear_dependencies $(SRCS)
  </code></pre>
  
  <p>But with including the <code>sources</code>-file, it never reaches the upper code block.</p>
  
  <p>Here are the constants defined at the top of the Makefile:</p>
  
  <pre><code>CXX = g++
  CXXFLAGS = -Wall \
         -Wextra \
         -Wuninitialized \
         -Wmissing-declarations \
         -pedantic \
         -O3 \
         -p -g -pg
  LDFLAGS =  -p -g -pg
  DEPFLAGS = -MM
  
  SRCDIR  = ./src
  OBJDIR  = ./obj
  
  TARGET = ./bin/my_funky_function_which_is_not_the_real_name
  
  -include sources
  
  OBJSTMP =   $(SRCS:.cpp=.o)
  OBJS        =   $(OBJSTMP:$(SRCDIR)=$(OBJDIR))
  DEP = depend.main
  EXT_SRC =   .cpp
  EXT_OBJ =   .o
  </code></pre>
  
  <p>What am I missing? Is my approach valid/feasible?</p>