title: how do i build libraries in subdirectories using cmake?
body: <p>My code is organized like this:</p>
  
  <ul>
  <li>cpp
  
  <ul>
  <li>main.cpp (calls code from <code>dataStructures/</code> and <code>common/</code>)</li>
  <li>CMakeLists.txt (<em>topmost</em> CMakeLists file)</li>
  <li>build</li>
  <li>common
  
  <ul>
  <li>CMakeLists.txt (should be responsible for building common shared library)</li>
  <li>include
  
  <ul>
  <li>utils.h</li>
  </ul></li>
  <li>src
  
  <ul>
  <li>utils.cpp</li>
  </ul></li>
  <li>build</li>
  </ul></li>
  <li>dataStructures
  
  <ul>
  <li>CMakeLists.txt (build dataStructures shared library - dependent on common library)</li>
  <li>include
  
  <ul>
  <li>dsLinkedList.h</li>
  </ul></li>
  <li>src
  
  <ul>
  <li>dsLinkedList.cpp</li>
  </ul></li>
  <li>build</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p><code>build\</code> directories contain the built target. The actual code can be seen here: <a href="https://github.com/brainydexter/PublicCode/tree/master/cpp" rel="noreferrer">https://github.com/brainydexter/PublicCode/tree/master/cpp</a></p>
  
  <p>As of now, CMakeLists.txt in each of the subdirectories build their own shared libraries. Topmost CMakeLists file then references the libraries and paths like this</p>
  
  <p><strong>Topmost CMakeLists.txt</strong></p>
  
  <pre><code>cmake_minimum_required(VERSION 3.2.2)
  project(cpp)
  
  #For the shared library:
  set ( PROJECT_LINK_LIBS libcppDS.dylib libcppCommon.dylib)
  link_directories( dataStructures/build )
  link_directories( common/build )
  
  #Bring the headers, into the project
  include_directories(common/include)
  include_directories(dataStructures/include)
  
  #Can manually add the sources using the set command as follows:
  set(MAINEXEC main.cpp)
  
  add_executable(testDS ${MAINEXEC})
  target_link_libraries(testDS ${PROJECT_LINK_LIBS} )
  </code></pre>
  
  <p><strong>How can I change the topmost CMakeLists.txt to go into subdirectories (<code>common</code> and <code>dataStructures</code>) and build their targets if they haven't been built, without me having to manually build the individual libraries ?</strong></p>
  
  <p>CMakeLists for <strong>common</strong> :</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.2.2)
  project(cpp_common)
  set(CMAKE_BUILD_TYPE Release)
  
  #Bring the headers, such as Student.h into the project
  include_directories(include)
  
  #However, the file(GLOB...) allows for wildcard additions:
  file(GLOB SOURCES "src/*.cpp")
  
  #Generate the shared library from the sources
  add_library(cppCommon SHARED ${SOURCES})
  </code></pre>
  
  <p><strong>dataStructures</strong> :</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.2.2)
  project(cpp_dataStructures)
  set(CMAKE_BUILD_TYPE Release)
  
  #For the shared library:
  set ( PROJECT_LINK_LIBS libcppCommon.dylib )
  link_directories( ../common/build )
  
  #Bring the headers, such as Student.h into the project
  include_directories(include)
  include_directories(../common/include/)
  
  #However, the file(GLOB...) allows for wildcard additions:
  file(GLOB SOURCES "src/*.cpp")
  
  #Generate the shared library from the sources
  add_library(cppDS SHARED ${SOURCES})
  </code></pre>
  
  <p>Update:</p>
  
  <p>This pull request helped me understand the correct way of doing this:
  <a href="https://github.com/brainydexter/PublicCode/pull/1" rel="noreferrer">https://github.com/brainydexter/PublicCode/pull/1</a></p>
  
  <p>and commitId: 4b4f1d3d24b5d82f78da3cbffe423754d8c39ec0 <a href="https://github.com/brainydexter/PublicCode/commit/4b4f1d3d24b5d82f78da3cbffe423754d8c39ec0" rel="noreferrer">on my git</a> </p>