title: CMake library dependencies
body: <p>This may be a tad hard to explain in full. I have setup a directory structure for a series of C++ libraries I am writing. I intend to use CMake to handle building these libraries. For the most part these libraries are completely separate "subsystems" but in the odd places one library needs access to the header file contained within another. Here is my directory structure.</p>
  
  <pre><code>base
   ├─ cmake
   ├─ docs
   ├─ examples
   └─ sources
      ├─ libA
      │   ├─ include
      │   │   └─ libA
      │   │       ├─ core.h
      │   │       └─ *.h
      │   ├─ source
      │   │   └─*.cpp
      └─ libB
          ├─ include
          │   └─ libB
          │       ├─ message.h
          │       └─ *.h
          └─ source
              └─ *.cpp
  </code></pre>
  
  <p>There are <code>CMakeLists.txt</code> files in a few places. The main one is within the root directory <code>base</code> which sets up a bunch of variables and options and lastly calls <code>add_subdirectory(sources)</code>. Within <code>sources</code> the <code>CMakeLists.txt</code> file simply calls:</p>
  
  <pre><code>add_subdirectory(libA)
  add_subdirectory(libB)
  </code></pre>
  
  <p>Lastly there are <code>CMakeLists.txt</code> files in both the <code>libA</code> and <code>libB</code> directories. These are both largely the same except for the target name. An example of one of these files is:</p>
  
  <pre><code>set(target libA)
  
  # Sources
  set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
  set(source_path  "${CMAKE_CURRENT_SOURCE_DIR}/source")
  
  # Add include directories
  include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
  
  # Set the source files to compile
  file(GLOB_RECURSE sources ${source_path}/**/*.cpp)
  
  # Build library
  add_library(${target} ${sources})
  </code></pre>
  
  <p>Now separately both of these libraries can be built without issue but I need to be able to include a header from <code>libA</code> in a source file within <code>libB</code></p>
  
  <pre><code>// base/sources/libB/source/message.cpp
  
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  
  #include &lt;libA/core.h&gt;
  #include &lt;libB/message.h&gt;
  </code></pre>
  
  <p>However when I build this I get the following error:</p>
  
  <pre><code>fatal error: 'libA/core.h' file not found
  </code></pre>
  
  <p>I have tried using <code>target_link_libraries</code> and <code>target_include_directories</code> without any luck. Clearly I am using these wrong or simply don't understand what each of these do. </p>