title: Recursive makefile for c++ source
body: <p>I've been struggling with makefiles for a while now, since I don't really get how they work...</p>
  
  <p>My directory structure is as followed:</p>
  
  <pre><code>--bin/
  --build/
  --includes/
  --src/
  ----classes/
  ------somefiles.hpp
  ------somefiles.cpp
  ----states/
  ------somestates.hpp
  ----main.cpp
  ----few-other-files.cpp
  --Makefile
  --.gitignore
  </code></pre>
  
  <p>Which worked fine with my makefile, until I started adding those subdirs, <code>classes</code> and <code>states</code></p>
  
  <p>I can't seem to find how to include those subdirs in my makefile which is:</p>
  
  <pre><code>CC := g++
  SRCDIR := src
  BUILDDIR := build
  TARGET := bin/game
  
  SRCEXT := cpp
  SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
  OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
  CFLAGS := -g -Wall
  LIB := -lsfml-audio -lsfml-graphics -lsfml-network -lsfml-system -lsfml-window
  INC := -I includes -I /usr/local/include -L /usr/local/lib
  
  $(TARGET): $(OBJECTS)
      @echo "Linking..."
      @echo "$(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB)
  
  $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
      @mkdir -p $(BUILDDIR)
      @echo "$(CC) $(CFLAGS) $(INC) -c -o $@ $&lt;"; $(CC) $(CFLAGS) $(INC) -c -o $@ $&lt;
  
  clean:
      @echo "Cleaning..."
      @echo "$(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)
  
  .PHONY: clean
  </code></pre>
  
  <p>Problem: <code>Fatal error: can't create build/classes/stateManager.o: No such file or directory</code></p>
  
  <p>Normally, the problem is that it can't build the directory. But I thought the <code>mkdir -p</code> would solve that.</p>
  
  <p>Any help is greatly appreciated.</p>