title: CMake and using git-submodule for dependence projects
body: <p>Consider the following three projects.</p>
  
  <p><code>ProjectA</code> does not have any dependencies and its <code>CMakeLists.txt</code> at top level is like the following,</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8.4)
  project(A CXX)
  add_library(a ${PROJECT_SOURCE_DIR}/liba.cpp)
  </code></pre>
  
  <p><code>ProjectB</code> depend on <code>ProjectA</code>, and I add <code>ProjectA</code> as a git-submodule, so its structure will be like below,</p>
  
  <ul>
  <li><code>ProjectB</code>
  
  <ul>
  <li><code>CMakeLists.txt</code></li>
  <li><code>libb.cpp</code></li>
  <li><code>ProjectA</code> (git submodule)
  
  <ul>
  <li><code>CMakeLists.txt</code></li>
  <li><code>liba.cpp</code></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>and <code>ProjectB</code>'s <code>CMakeLists.txt</code> look like the following</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8.4)
  project(B CXX)
  add_subdirectory(ProjectA)
  add_library(b ${PROJECT_SOURCE_DIR}/libb.cpp)
  target_link_libraries(b a)
  </code></pre>
  
  <p>So far it is alright.</p>
  
  <p>Now let's say it comes a <code>ProjectC</code>. It depends on both <code>ProjectA</code> and <code>ProjectB</code>. And let's assume that I am not aware that <code>ProjectB</code> depends on <code>ProjectA</code> already (e.g., I did not create the two before. Or think that <code>ProjectC</code> actually have many dependencies and I shall not be forced to figure out an exact dependency tree among them).</p>
  
  <p>Anyway, I add both <code>ProjectA</code> and <code>ProjectB</code> as git submodules in <code>ProjectC</code>. So it has the following structure,</p>
  
  <ul>
  <li><code>ProjectC</code>
  
  <ul>
  <li><code>CMakeLists.txt</code></li>
  <li><code>libc.cpp</code></li>
  <li><code>ProjectA</code> (git submodule)
  
  <ul>
  <li><code>CMakeLists.txt</code></li>
  <li><code>liba.cpp</code></li>
  </ul></li>
  <li><code>ProjectB</code> (git submodule)
  
  <ul>
  <li><code>CMakeLists.txt</code></li>
  <li><code>libb.cpp</code></li>
  <li><code>ProjectA</code> (git submodule of the submodule <code>ProjectB</code>)
  
  <ul>
  <li><code>CMakeLists.txt</code></li>
  <li><code>liba.cpp</code></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>And it has the following <code>CMakeLists.txt</code>.</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8.4)
  project(C CXX)
  add_subdirectory(ProjectA)
  add_subdirectory(ProjectB)
  add_library(c ${PROJECT_SOURCE_DIR}/libc.cpp)
  target_link_libraries(c a b)
  </code></pre>
  
  <p>Now if I try to run <code>cmake</code> for <code>ProjectC</code>, I get the following error.</p>
  
  <pre><code>add_library cannot create target "a" because another target with the same
  name already exists....
  </code></pre>
  
  <p>I understand the reason of this error. It is because <code>ProjectA</code> is added as a subdirectory twice and all targets created by <code>add_library</code> is <strong>Global</strong>. For this particular case, I can fix it by remove <code>add_subdirectory(ProjectA)</code> in the <code>ProjectC/CMakeLists.txt</code>. However, consider a situation that <code>ProjectC</code> has many dependencies, and there might or might not be dependencies among them. From point of view of the developer of <code>ProjectC</code>, he should not need to care about the inter-dependencies among its own dependencies.</p>
  
  <p>In this situation, what is the best way to have <code>ProjectC</code> include its dependencies? Having <code>ProjectA</code> and <code>ProjectB</code> as a git-submodule in source form is a must. I am aware that I can simply install <code>ProjectA</code> and <code>ProjectB</code> somewhere, and <code>ProjectC</code> only need to find the installed files somewhere. However, if possible, I would like to avoid that kind of solution (for example, if the installation was built with a different ABI than the one used by <code>ProjectC</code>, incompatibility issues arise). I would like all three projects to be built inside the build tree of <code>ProjectC</code>.</p>