title: Handling header files dependencies with cmake
body: <p>I am using CMake on a small C++ project and so far it works great... with one twist :x</p>
  <p>When I change a header file, it typically requires recompiling a number of sources files (those which include it, directly or indirectly), however it seems that cmake only detects <em>some</em> of the source files to be recompiled, leading to a corrupted state. I can work around this by wiping out the project and rebuilding from scratch, but this circumvents the goal of using a make utility: only recompiling what is needed.</p>
  <p>Therefore, I suppose I am doing something wrong.</p>
  <p>My project is very simply organized:</p>
  <ul>
  <li>a top directory where all resources sit, the main CMakeLists.txt sits there</li>
  <li>a &quot;include&quot; directory where all public headers lies (in various subdirectories)</li>
  <li>a &quot;src&quot; directory where all the subdirectories for sources files are, the src CMakeLists.txt sits there</li>
  <li>a CMakeLists.txt per subdirectory in the &quot;src&quot; directory</li>
  </ul>
  <p>The main directory has:</p>
  <pre><code>cmake_minimum_required(VERSION 2.8)
  
  project(FOO)
  
  set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
  
  # Compiler Options
  set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -g -std=c++0x -Wall -Wextra -Werror&quot;)
  
  include_directories($(FOO_SOURCE_DIR)/include)
  
  add_subdirectory(src)
  </code></pre>
  <p>The &quot;src&quot; directory:</p>
  <pre><code>add_subdirectory(sub1)
  add_subdirectory(sub2)
  add_subdirectory(sub3)
  add_subdirectory(sub4)
  
  add_executable(foo main.cpp)
  
  target_link_libraries(foo sub1 sub2 sub3 sub4)
  </code></pre>
  <p>Where <code>sub4</code> depends on <code>sub3</code> which depends on <code>sub2</code> which depends on <code>sub1</code></p>
  <p>And an example of a subdirectory (sub3):</p>
  <pre><code>set(SUB3_SRCS
      File1.cpp
      File2.cpp
      File3.cpp
      File4.cpp
      File5.cpp
      File6.cpp
      )
  
  add_library(sub3 ${SUB3_SRCS})
  
  target_link_libraries(sub3 sub1 sub2)
  </code></pre>
  <p>I'd be glad if anyone could point my mistake to me, searching here or on CMake didn't yield anything so I guess it's very easy or should work out of the box...</p>
  <p>(for reference, I am using cmake version 2.8.2 on MSYS)</p>
  <p><strong>EDIT</strong>:</p>
  <p>Thanks to Bill's suggestion I have checked the <code>depend.make</code> file generated by CMake, and it is indeed lacking (severely). Here is an example:</p>
  <pre><code>src/sub3/CMakeFiles/sub3.dir/File1.cpp.obj: ../src/sub3/File1.cpp
  </code></pre>
  <p>Yep, that's all, none of the includes were referenced at all :x</p>