title: make one static library from whole project with cmake
body: <p>c++-project, say, <code>foo</code> is maintained by the cmake.
  One wants to  create one library <code>libfoo.a</code> (with all classes/methods/functions created at the whole source-tree) to make possible creating programs that could linked to the library with <code>-lfoo</code>.</p>
  
  <p>ok, let's consider now a toy example, and the prolbem will be clear. Directory <code>foo</code> (root of the project) contains directories <code>a</code>, and <code>b</code>. Two <code>CmakeLists.txt</code> are created:</p>
  
  <pre><code># a/CMakeLists.txt
  add_library(A &lt;a_sources&gt;)
  # b/CMakeLists.txt
  add_library(B &lt;b_sources&gt;)
  </code></pre>
  
  <p>And one <code>CMakeLists.txt</code> for root directory:</p>
  
  <pre><code>add_subdirectory(a)
  add_subdirectory(b)
  add_library(foo &lt;foo_sources&gt;
  target_link_libraries(foo A B)
  </code></pre>
  
  <p>That was a surprise for me: <strong>after building libfoo.a contains only methods from foo_sources</strong>, and <code>a_sources</code>,<code>b_sources</code> are excluded.
  That is ok in the case when executables are built with the same project: while creating executables cmake "guesses" that <code>a</code> and <code>b</code> must be linked if it is linked to <code>foo</code>.
  But in the case executable is created "outside" project to use library <code>foo</code> one must link with <code>-lfoo -la -lb</code>, now imagine a project with lots of subdirectories - how to deal with it? so question is "how to create one library, aggregating methods from whole project with means of cmake?"</p>
  
  <p>Googling led me to relatively recently embedded (appeared in 2.8.8) <code>OBJECT library</code> opportunity. Nice example of using it is shown <a href="http://www.vtk.org/Wiki/CMake/Tutorials/Object_Library">here</a>. Now the problem above can be solved with that:</p>
  
  <pre><code># a/CMakeLists.txt
  add_library(A OBJECT &lt;a_sources&gt;)
  # b/CMakeLists.txt
  add_library(B OBJECT &lt;b_sources&gt;)
  # foo/CMakeLists.txt
  add_subdirectory(a)
  add_subdirectory(b)
  add_library(foo &lt;foo_sources&gt; $&lt;TARGET_OBJECTS:A&gt; $&lt;TARGET_OBJECTS:B&gt;)
  </code></pre>
  
  <p>problem seems to be solved, unfortunately, not quite.</p>
  
  <p>if dependency chain is longer than 2, for example, <code>foo</code> depends on <code>A</code>, which depends on <code>B</code>, problem still remains.
  That is because, </p>
  
  <blockquote>
    <p>Object libraries may contain only sources (and headers) that compile to object files.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>Object libraries cannot be imported, exported, installed, or linked.</p>
  </blockquote>
  
  <p>(quotes are taken from the same <a href="http://www.vtk.org/Wiki/CMake/Tutorials/Object_Library#Restrictions">link</a>)</p>
  
  <p>I've tried several combinations of <code>target_link_library()</code>, <code>add_library(), add_library(... OBJECT ..)</code> trying to link <code>A</code> and <code>B</code> to <code>foo</code> without success (error during cmake-process.)</p>
  
  <p>I must be loosing something simple, please help, thank you!
  I am not sure is it important: project is maintained at the linux.</p>