title: CMake library linking order
body: <p>I have following libraries <code>lib_A</code>, <code>lib_B</code>, <code>lib_C</code>, <code>lib_D</code>. I am doing something like this in my CMake files (order is important):</p>
  
  <ol>
  <li><code>ADD_LIBRARY(lib_A)</code></li>
  <li><code>ADD_LIBRARY(lib_B)</code></li>
  <li><code>ADD_LIBRARY(lib_C)</code></li>
  <li><code>ADD_LIBRARY(lib_D)</code></li>
  <li><code>TARGET_LINK_LIBRARIES(lib_B lib_C)</code></li>
  <li><code>TARGET_LINK_LIBRARIES(lib_A lib_B)</code></li>
  <li><code>ADD_EXECUTABLE(Exec)</code></li>
  <li><code>TARGET_LINK_LIBRARIES(exec lib_A)</code></li>
  <li><code>TARGET_LINK_LIBRARIES(exec lib_D)</code></li>
  </ol>
  
  <p>This results in following linker command.</p>
  
  <pre><code>linker -llib_A -llib_D -llib_B -llib_C
  </code></pre>
  
  <p>Q1. Why are <code>lib_B</code> and <code>lib_C</code> after <code>lib_D</code>?</p>
  
  <p>Q2. When I change CMake a little bit and do something like this:</p>
  
  <ol start="8">
  <li><code>TARGET_LINK_LIBRARIES(lib_A lib_D)</code></li>
  <li><code>TARGET_LINK_LIBRARIES(exec lib_A)</code></li>
  </ol>
  
  <p>then linking order is like this:</p>
  
  <pre><code>linker -llib_A -llib_B -llib_C -llib_D
  </code></pre>
  
  <p>Here, <code>lib_B</code> and <code>lib_C</code> are before <code>lib_D</code>. It means that <code>target_link_libraries</code> works differently for executable targets and library targets. Am I right?</p>
  
  <p>The problem here is that <code>lib_B</code> and <code>lib_C</code> also depend on <code>lib_D</code>, but I don't want to make <code>target_link_libraries(lib_B lib_D)</code> and <code>target_link_libraries(lib_C lib_D)</code>, because I have more of such cases, and I would have to do it manually for each library.
  Of course doing like in Q2 solves the problem but:</p>
  
  <p>Q3 - Is this order guaranteed somehow by CMake or it is just a fortuity?</p>
  
  <p>Thank you</p>