title: CMake: relink when linked package is rebuilt
body: <p>I apologize if this has been asked already but I can't find online the right way to get this thing to work.</p>
  
  <p>I have a cmake project Foo which depends on another cmake project Bar. Goal: whenever Bar is reinstalled (changing only the libraries, not the headers), then Foo should re-link (without rebuilding, of course).</p>
  
  <p>So, in the CMakeLists.txt in the top folder of project Foo (which has only one target, an executable) I have the cmake command</p>
  
  <pre><code>FIND_PACKAGE(Bar REQUIRED)
  </code></pre>
  
  <p>Which, correctly, finds project Bar at configure time. In the part where I create the target, I have</p>
  
  <pre><code>LINK_DIRECTORIES (${BAR_LIBRARY_DIRS} )
  ADD_EXECUTABLE(foo.exe main.cpp)
  TARGET_LINK_LIBRARIES(foo.exe ${BAR_LIBRARIES})
  </code></pre>
  
  <p>The two variables in there are defined in BarConfig.cmake, which is the one that <code>FIND_PACKAGE(Bar)</code> looks for, and contains only the following instructions</p>
  
  <pre><code>SET(BAR_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../../include")
  SET(BAR_LIBRARY_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../../lib")
  SET(BAR_LIBRARIES bar)
  </code></pre>
  
  <p>I printed on screen those variables and they contain what you expect them to contain (/path/to/folder/include, /path/to/folder/lib and bar).</p>
  
  <p>So, I read somewhere that cmake cannot add a dependency to a library which is not specified with its complete path. One should write</p>
  
  <pre><code>TARGET_LINK_LIBRARIES (foo.exe full-path-to-bar-libraries)
  </code></pre>
  
  <p>In that case, it works. But it's unsatisfactory. First, cause the path can change. But you can read it from a variable, you might say. True. But, second, even in that case, if the project Bar contains a number of libraries that is not known, one would have to sweat to create the correct string to add there...</p>
  
  <p>However, I also read that if that library is also built and installed with cmake, it should work automatically. As a matter of fact, I have another project pair, A depends on B, both built with cmake. In that case the dependence works. Unfortunately, the project B is HUGE, and defines TONS of cmake macros, and I can't identify the part where it sets up the right variables.</p>
  
  <p>Do you have any idea of how to get Foo to re-link (without rebuilding) every time that the library Bar is reinstalled? I would like to avoid to use full path.</p>
  
  <p>Thanks</p>
  
  <p>Edit: to be more clear: if library Bar set up a variable BAR_LIBRARIES containing all its libraries with the full path, then TARGET_LINK_LIBRARIES would work. However, most likely BAR_LIBRARIES would contain 'bar', rather than '/some/path/libbar.a'. I'd like cmake to be able, given the directories provided with LINK_DIRECTORIES and the library names provided with TARGET_LINK_LIBRARIES to put the two pieces together. E.g. if BAR_LIBRARY_DIRS contains '/folder1/;/folder2/' and BAR_LIBRARIES contains 'bar1;bar2', I'd like cmake to establish a dependency on libbar1.a and libbar2.a, found in any of the following:</p>
  
  <ol>
  <li>/folder1/libbar1.a</li>
  <li>/folder2/libbar1.a</li>
  <li>/folder1/libbar2.a</li>
  <li>/folder2/libbar2.a</li>
  </ol>
  
  <p>And relink if any of the ones upon which a dependency has been created is changed since the last linking.</p>