title: CMake: Redundant linking when modifying shared libraries
body: <p>I work on a project comprised of a couple dozen shared libraries, each of which has many associated unit tests. Many libs are also dependent on other libs because a lib for some specific functionality will use the code from one of the more common libs. And finally of course there are the production executables which depend on the libs.</p>
  
  <p>There is no question that a change in the API (a header file) of some core common lib should trigger a major recompilation of nearly the entire system. But often there is only a change in the implementation, and the only file compiled is the modified .cxx, and in theory just the modified lib would need to be linked - thanks to dynamic linking there should be no need to relink anything else. But CMake goes ahead and does it anyway: after relinking the lib it relinks all the unit tests associated with that lib. Then it relinks all the libs in that lib's dependency tree and all their unit tests. Finally it relinks the production executables. Due to the scale of the project this takes a lot of precious time.</p>
  
  <p>I have reproduced this behavior with a simple project based on <a href="https://cmake.org/examples/" rel="noreferrer">this</a> minimal example (comments removed for brevity and lib changed to shared). My system is Ubuntu 16 on an Intel PC and my CMake version is 3.5.1.</p>
  
  <p>Start with an empty directory and create these files:</p>
  
  <p>CMakeLists.txt</p>
  
  <pre><code>cmake_minimum_required (VERSION 2.8.11)
  project (HELLO)
  add_subdirectory (Hello)
  add_subdirectory (Demo)
  </code></pre>
  
  <p>Demo/CMakeLists.txt</p>
  
  <pre><code>add_executable (helloDemo demo.cxx)
  target_link_libraries (helloDemo LINK_PUBLIC Hello)
  </code></pre>
  
  <p>Demo/demo.cxx</p>
  
  <pre><code>#include "hello.h"
  int main() { hello(); }
  </code></pre>
  
  <p>Hello/CMakeLists.txt</p>
  
  <pre><code>add_library (Hello SHARED hello.cxx)
  target_include_directories (Hello PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  </code></pre>
  
  <p>Hello/hello.h</p>
  
  <pre><code>void hello();
  </code></pre>
  
  <p>Hello/hello.cxx</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  void hello() { printf("hello!\n"); }
  </code></pre>
  
  <p>now run the commands:</p>
  
  <pre><code>mkdir build
  cd build
  cmake ../
  make
  </code></pre>
  
  <p>You may now execute <code>Demo/helloDemo</code> and see <code>hello!</code>.</p>
  
  <p>Now, <code>touch</code> Hello/hello.cxx and <code>make</code> again. You will see that the <code>helloDemo</code> executable is relinked ("<code>Linking CXX executable helloDemo</code>"). Even if hello.cxx is modified to print a different string the relinked executable remains binary identical so really the relinking was unnecessary.</p>
  
  <p>Is there a way to prevent these redundant build actions?</p>