title: Makefile: Compiling from directory to another directory
body: <p>I am trying to use Makefile to compile a bunch of <code>.cpp</code> files located in <code>src/code/*.cpp</code>, then compile each <code>*.o</code> in <code>build/</code>, and finally generate executable with those in <code>build/</code> as well.</p>
  
  <p>I have read a couple answers that I tried to work with but have encountered issues I do not understand.</p>
  
  <pre><code>CC = g++
  FLAGS = -g -c
  
  SOURCEDIR = /src/code
  BUILDDIR = build
  
  EXECUTABLE = DesktopSpecificController
  SOURCES = $(wildcard src/code/*.cpp)
  OBJECTS = $(patsubst src/code/*.cpp,build/%.o,$(SOURCES))
  
  all: dir $(BUILDDIR)/$(EXECUTABLE)
  
  dir:
      mkdir -p $(BUILDDIR)
  
  $(BUILDDIR)/$(EXECUTABLE): $(OBJECTS)
      $(CC) $^ -o $@
  
  $(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp
      $(CC) $(FLAGS) $&lt; -o $@
  
  clean:
      rm -f $(BUILDDIR)/*o $(BUILDDIR)/$(EXECUTABLE)
  </code></pre>
  
  <p>I do get the following error, and I am not sure why:</p>
  
  <pre><code>Makefile:19: target `src/code/main.cpp' doesn't match the target pattern
  </code></pre>
  
  <p>I also see that when trying to build the EXECUTABLE, it is not using the .o files, so it seems my rule is wrong here.</p>