title: CMake: ordering of include directories (How to mix system- and user-based include paths?)
body: <p>I've got a CMake project that includes and links against two libraries, say <code>A</code> and <code>B</code> (actually it's more than two and one of them is boost stuff, but that doesn't really matter here). Both are located via <code>FindSomething.cmake</code> scripts that (correctly) populate the standard CMake variables such that include directories are added via</p>
  
  <pre><code>INCLUDE_DIRECTORIES(${A_INCLUDE_DIRS})
  INCLUDE_DIRECTORIES(${B_INCLUDE_DIRS})
  </code></pre>
  
  <p>and linking is later done via</p>
  
  <pre><code>TARGET_LINK_LIBRARIES(mytarget ${A_LIBRARIES} ${B_LIBRARIES})
  </code></pre>
  
  <p>Now, the problem is that both libraries can either reside in a user based location or in the system directories (I'm on linux by the way, CMake 2.8.2) - or in both. Let's say <code>A</code> is only in <code>$HOME/usr/include</code> and <code>$HOME/usr/lib</code> while <code>B</code> (boost in my case) resides in both the system paths (<code>/usr/include</code> and <code>/usr/lib</code>) AND in the user based paths - in different versions. The find scripts can be made to find either the system or the user-based library <code>B</code>, this works.</p>
  
  <p>The trouble starts when I want to link against <code>B</code> from the system paths.<code>${B_INCLUDE_DIRS}</code> and <code>${B_LIBRARIES}</code> correctly point to the system-wide locations of the headers and libraries. But there is still <code>${A_INCLUDE_DIRS}</code> that points to a non-system include directory and ultimately also the headers for library <code>B</code> are taken from this location, while the linking for <code>B</code> uses the version from the system paths (via <code>${B_LIBRARIES}</code>) which leads to conflicts, i.e. linking errors.</p>
  
  <p>Changing the order of the <code>INCLUDE_DIRECTORIES</code> statements does not seem to change anything. I checked the origin of the symbols that cause the linking errors via <code>nm --line-numbers</code> on the object files.</p>
  
  <p>What can I do? Is there a trick to </p>
  
  <ul>
  <li>force the ordering of the include directories (even if this would mean to give precedence to a system path although there is also a user-based location specified)?</li>
  <li>tell CMake to use <code>${A_INCLUDE_DIRS}</code> for all headers from <code>A</code> and <code>${B_INCLUDE_DIRS}</code> for all headers from <code>B</code>?</li>
  </ul>