title: Makefile - Dependency file in a folder "No such file or directory"
body: <p>I've got a Makefile that works good so far. Although, as it started growing, recompiling all the sources every time began to take too long. Here's a snippet from the working version:</p>
  
  <pre><code>CC=$(CROSS_COMPILE)g++
  CFLAGS=-Wall -I./include -pg -O2
  VPATH=./src:./include
  
  all: dotgazer.cpp dotgazer/Dot.cpp
      $(CC) $(CFLAGS) $^ -o dotgazer.out `pkg-config --libs opencv`
  </code></pre>
  
  <p>There are a lot more dependencies, but these two are enough to show what the problem is. I'm trying to move the compilation stage of each <code>cpp</code> file to a separate target. When it comes to the top-level file (<code>dotgazer.cpp</code>) it's not a problem and a general rule <code>%.o: %.cpp</code> works fine. But I can't get the second dependency to work. Here's how it looks now:</p>
  
  <pre><code>CC=$(CROSS_COMPILE)g++
  CFLAGS=-Wall -I./include -pg -O2
  VPATH=./src:./include
  
  all: dotgazer.o dotgazer/Dot.o
      $(CC) $(CFLAGS) $^ -o dotgazer.out `pkg-config --libs opencv`
  
  %.o: %.cpp
      $(CC) -c $(CFLAGS) $^ -o $@
  
  dotgazer/Dot.o: dotgazer/Dot.cpp
      $(CC) -c $(CFLAGS) $^ -o $@
  </code></pre>
  
  <p>I've tried different variations of the <code>Dot.o</code> rule but none of them seems to work. The error I get with the one above is:</p>
  
  <pre><code>Fatal error: can't create dotgazer/Dot.o: No such file or directory
  </code></pre>
  
  <p>How should I do that? I'd most prefer to have the <code>.o</code> files in the same folders as their sources. Also, I'd be thankful for general rules (like <code>%.o: %.cpp</code>) as there are lots of source files and I don't want the Makefile to get too bloated. Thank you!</p>