title: Makefile, find sources in src directory tree and compile to .o in build file
body: <p>I'm currently trying to re-write a makefile for a project that I've inherited to make the source tree neater and easier to work with. At the moment the source tree is something like this:</p>
  
  <pre><code> Project/
   ----bin/
   ----build/
   ----include/
       ----main.h
       ----part1.h
       ----part2.h
       ----part3.h
   ----src/
       ----main.cpp
       ----part1.cpp
       ----Subdir/
           ----part2.c
       ----Subdir2/
           ----part3.cpp
  </code></pre>
  
  <p>What I want is a rule in my makefile that will find all of the .cpp and .c files in my src directory and compile them to a flat directory of objects in the build directory. At the moment I have the following in my makefile but this seems to miss a number of the cpp files:</p>
  
  <pre><code>BUILDDIR = build
  
  $(BUILDDIR)/%.o :       src/**/%.cpp | $(BUILDDIR)
  $(BUILDDIR)/%.o :       src/%.cpp    | $(BUILDDIR)
      $(CXX) $(CFLAGS) -c $&lt; -o $@ $(INCS)
  
  $(BUILDDIR)/%.o :       src/**/%.c   | $(BUILDDIR)
  $(BUILDDIR)/%.o :       src/%.c      | $(BUILDDIR)
      $(CC)  $(CFLAGS) -c $&lt; -o $@ $(INCS)
  </code></pre>
  
  <p>At the moment when I run make -n it seems that it has detected main.cpp and part1.cpp but none of the ones in subdirectories. Make then goes on to try and load the files according to later rules in the Makefile.</p>
  
  <p>Due to the number of files in the project I'd rather not write a list of them manually but if it comes to that I might have to.</p>