title: Using cmake to create main and sub-projects, and be able to compile them individually
body: <p><strong>DISCLAIMER:</strong> I am new to CMakeLists.txt, I have a working implementation and would like to improve and enhance it, problem described below:</p>
  
  <p>If I want <code>root/sub-directories/</code> as separate sub-projects that can be compiled using the individual <code>CMakeLists.txts</code> in their folders I find myself literally copy pasting almost the entire root file <code>CMakeLists.txt</code> per sub-directory.</p>
  
  <p>I was wondering if there is a better way to have a main project and then sub-projects that get the shared dependencies from main project and can be compiled without <code>cmake</code>-ing the root <code>CMakeLists.txt</code>. My directory structure is;</p>
  
  <p>CMakeLists.txt (root project)<br>
  | __ sub_dir-1<br>
  | __ | __ CMakeLists.txt (sub-project)<br>
  | __ sub_dir-2<br>
  | __ | __ CMakeLists.txt (sub-project)<br>
  | __ sub_dir-3<br>
  | __ | __ CMakeLists.txt (sub-project)<br>
  ...<br></p>
  
  <p>Essentially, I want to be able to:</p>
  
  <ol>
  <li><code>cmake root/CMakeLists.txt</code>, which creates an entire project that includes the sub-projects (I already have this implemented using individual CMakeLists.txts inside sub-directories.</li>
  <li><p><code>cmake root/sub-dir/CMakeLists.txt</code> and <strong>only</strong> compile the sub-project, which essentially also finds the necessary dependencies and includes them from maybe <code>.cmake</code> includes or <code>root/CMakeLists.txt</code>. What is the best way to approach this structure;</p>
  
  <ul>
  <li>while retaining the first bullet point of having the ability to compile it as an entire project.</li>
  <li>And also not <em>crowding</em> the <code>sub-dir/CMakeLists.txt</code> too much with redundant code? </li>
  </ul></li>
  </ol>
  
  <p>Appreciate any suggestions! Thank you.</p>