title: Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs
body: <h2>Update 2</h2>
  
  <p>After messing around a bit (and some editing of the generated Makefiles), it looks like what is happening is that moc is not properly processing <code>MainWindow.h</code> (included by <code>main.cpp</code> and <code>MainWindow.cpp</code> unless it is in the same folder as the source files which included it. </p>
  
  <p>Moc runs on <code>MainWindow.cpp</code>, doesn't process the include and thus doesn't see the Q_OBJECT macro so proceeds to produce an empty output file. I'm not sure whether moc usually processes includes or if it just scans the directory, but either way, headers that need mocing but are in other directories are not being processed!</p>
  
  <h2>Update</h2>
  
  <p>The problem appears to be related to the output produced by moc. In the first case (the one that compiles), <code>hello-world_automoc.cpp</code> and <a href="http://pastebin.com/2JLLN1B8" rel="noreferrer"><code>moc_MainWindow.cpp</code></a> are generated. <code>hello-world_automoc.cpp</code> looks like</p>
  
  <pre><code>/* This file is autogenerated, do not edit*/
  #include "moc_MainWindow.cpp"
  </code></pre>
  
  <p>In the second case, a <code>hello-world_automoc.cpp</code> is produced that looks like</p>
  
  <pre><code>/* This file is autogenerated, do not edit*/
  enum some_compilers { need_more_than_nothing };
  </code></pre>
  
  <p>and there is no <code>moc_MainWindow.cpp</code> at all. If I manually call moc in from cmake instead of using automoc in the broken case, I do get <code>moc_MainWindow.cpp</code> but it is empty.</p>
  
  <h2>Original Status</h2>
  
  <p>Firstly, no, I haven't forgotten to <a href="https://stackoverflow.com/questions/14015792/why-am-i-getting-undefined-reference-to-vtable-errors-when-linking-this-qt"><code>set(CMAKE_AUTOMOC ON)</code></a>. Also note that <code>MainWindow</code>'s <a href="https://stackoverflow.com/questions/15309869/undefined-reference-to-vtable">destructor is declared and implemented</a>.</p>
  
  <p>When my directory structure looks like:
  </p>
  
  <pre>CMakeLists.txt
  |__ <a href="http://pastebin.com/t6MF2wU5" rel="noreferrer">main.cpp</a>
  |__ <a href="http://pastebin.com/BWk64UJp" rel="noreferrer">MainWindow.cpp</a>
  |__ <a href="http://pastebin.com/gys3K0Cm" rel="noreferrer">MainWindow.h</a>
  |__ <a href="http://pastebin.com/endTAxxi" rel="noreferrer">MainWindow.ui</a>
  </pre>
  
  <p>compiling works just fine.</p>
  
  <p>However, when it looks like:</p>
  
  <pre>helloworld/
  |__ <a href="http://pastebin.com/91ZUz7rH" rel="noreferrer">CMakeLists.txt</a>
  |__ src/
  |   |__ <a href="http://pastebin.com/7r2mGmJ1" rel="noreferrer">CMakeLists.txt</a>
  |   |__ main.cpp
  |   |__ MainWindow.cpp
  |
  |__ inc/
  |   |__ MainWindow.h
  |
  |__ gui/
      |__ MainWindow.ui
  </pre>
  
  <p>I get linking errors:</p>
  
  <pre><code>Linking CXX executable hello-world
  CMakeFiles/hello-world.dir/MainWindow.cpp.o: In function `MainWindow::MainWindow()':
  MainWindow.cpp:(.text+0x3b): undefined reference to `vtable for MainWindow'
  MainWindow.cpp:(.text+0x4d): undefined reference to `vtable for MainWindow'
  CMakeFiles/hello-world.dir/MainWindow.cpp.o: In function `MainWindow::~MainWindow()':
  MainWindow.cpp:(.text+0xaf): undefined reference to `vtable for MainWindow'
  MainWindow.cpp:(.text+0xc1): undefined reference to `vtable for MainWindow'
  collect2: error: ld returned 1 exit status
  make[2]: *** [src/hello-world] Error 1
  make[1]: *** [src/CMakeFiles/hello-world.dir/all] Error 2
  </code></pre>
  
  <p>I'd really like to have my sources and headers in proper subdirectories, but I'm not quite sure how to fix this.</p>
  
  <p>This is actually the simplest identifiable case of an error from a much larger project, so I'm really not all that keen to flatten the project directories just because I'm adding a Qt GUI to it.</p>