title: CMake target_include_directories relative to parent directory?
body: <p>How do I add <code>lib0</code> path to <code>lib1</code> such that from <code>header1.h</code> I can <code>#include &lt;header0.h&gt;</code>?</p>
  <pre><code>|   CMakeLists.txt
  +---lib0
  |       CMakeLists.txt
  |       header0.c
  |       header0.h
  +---lib1
  |       CMakeLists.txt
  |       header1.c
  |       header1.h
  \---main
          CMakeLists.txt
          main.c
  </code></pre>
  <h3 id="cmakelists.txt-2erd"><code>CMakeLists.txt</code></h3>
  <pre class="lang-cmake prettyprint-override"><code>cmake_minimum_required(VERSION 3.19)
  project(c_multilib C)
  
  foreach (sub &quot;lib0&quot; &quot;lib1&quot; &quot;main&quot;)
      add_subdirectory(&quot;${sub}&quot;)
  endforeach ()
  </code></pre>
  <h3 id="lib0cmakelists.txt-9b57"><code>lib0/CMakeLists.txt</code></h3>
  <pre class="lang-cmake prettyprint-override"><code>add_library(lib0 &quot;header0.h&quot; &quot;header0.c&quot;)
  
  set_target_properties(lib0 PROPERTIES LINKER_LANGUAGE C)
  
  include(GenerateExportHeader)
  set(_generated_export_header &quot;${CMAKE_CURRENT_SOURCE_DIR}/lib0_export.h&quot;)
  generate_export_header(lib0 EXPORT_FILE_NAME &quot;${_generated_export_header}&quot;)
  
  target_include_directories(lib0
          INTERFACE
          &quot;$&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}&gt;&quot;
          &quot;$&lt;INSTALL_INTERFACE:include&gt;&quot;
  )
  
  install(FILES &quot;header0.h&quot; &quot;${_generated_export_header}&quot; DESTINATION &quot;include&quot;)
  </code></pre>
  <h3 id="lib0header0.h-mpbu"><code>lib0/header0.h</code></h3>
  <pre class="lang-c prettyprint-override"><code>#ifndef C_MULTILIB_HEADER0_H
  #define C_MULTILIB_HEADER0_H
  
  #include &quot;lib0_export.h&quot;
  
  int LIB0_EXPORT sum(int, int);
  
  #endif /* C_MULTILIB_HEADER0_H */
  </code></pre>
  <h3 id="lib0header0.c-xf97"><code>lib0/header0.c</code></h3>
  <pre class="lang-c prettyprint-override"><code>#include &quot;header0.h&quot;
  
  int sum(int a, int b) {return a+b;}
  </code></pre>
  <h3 id="lib1cmakelists.txt-ph1a"><code>lib1/CMakeLists.txt</code></h3>
  <pre class="lang-cmake prettyprint-override"><code>add_library(lib1 &quot;header1.h&quot; &quot;header1.c&quot;)
  
  set_target_properties(lib1 PROPERTIES LINKER_LANGUAGE C)
  
  include(GenerateExportHeader)
  set(_generated_export_header &quot;${CMAKE_CURRENT_SOURCE_DIR}/lib1_export.h&quot;)
  generate_export_header(lib1 EXPORT_FILE_NAME &quot;${_generated_export_header}&quot;)
  
  target_include_directories(lib1
          INTERFACE
          &quot;$&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}&gt;&quot;
          &quot;$&lt;BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/lib0&gt;&quot;
          &quot;$&lt;INSTALL_INTERFACE:include&gt;&quot;
  )
  
  install(FILES &quot;header1.h&quot; &quot;${_generated_export_header}&quot; DESTINATION &quot;include&quot;)
  </code></pre>
  <h3 id="lib1header1.h-a0tg"><code>lib1/header1.h</code></h3>
  <pre class="lang-c prettyprint-override"><code>#ifndef C_MULTILIB_HEADER1_H
  #define C_MULTILIB_HEADER1_H
  
  #include &lt;header0.h&gt;
  #include &quot;lib1_export.h&quot;
  
  int LIB1_EXPORT add1(int);
  
  #endif /* C_MULTILIB_HEADER1_H */
  </code></pre>
  <h3 id="lib1header1.c-u2al"><code>lib1/header1.c</code></h3>
  <pre class="lang-c prettyprint-override"><code>#include &lt;header1.h&gt;
  
  int add1(int a) {return sum(a, 1);}
  </code></pre>
  <h3 id="maincmakelists.txt-4r1u"><code>main/CMakeLists.txt</code></h3>
  <pre class="lang-cmake prettyprint-override"><code>add_executable(&quot;main&quot; &quot;main.c&quot;)
  
  target_link_libraries(&quot;main&quot; &quot;lib0&quot; &quot;lib1&quot;)
  target_include_directories(
          &quot;main&quot;
          INTERFACE
          &quot;$&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}&gt;&quot;
          &quot;$&lt;BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/lib0&gt;&quot;
          &quot;$&lt;BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/lib1&gt;&quot;
          &quot;$&lt;INSTALL_INTERFACE:include&gt;&quot;
  )
  </code></pre>
  <h3 id="mainmain.c-hqbo"><code>main/main.c</code></h3>
  <pre class="lang-c prettyprint-override"><code>#include &lt;header0.h&gt;
  #include &lt;header1.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void) {
      printf(&quot;sum(1,5) =\t%d\n&quot;
                     &quot;add1(5) =\t%d\n&quot;, sum(1,5), add1(6));
      return EXIT_SUCCESS;
  }
  </code></pre>
  <p>Attempts:</p>
  <pre class="lang-cmake prettyprint-override"><code># lib1/CMakeLists.txt
  
  target_include_directories(
      lib1
      INTERFACE
      &quot;$&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}&gt;&quot;
      #&quot;$&lt;BUILD_INTERFACE:lib0&gt;&quot;
      #&quot;$&lt;BUILD_INTERFACE:../../lib0&gt;&quot;
      #&quot;$&lt;BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/src/lib0&gt;&quot;
      &quot;$&lt;INSTALL_INTERFACE:include&gt;&quot;
  )
  
  install(FILES &quot;header1.h&quot; DESTINATION &quot;include&quot;)
  </code></pre>
  <p>Errors range from:</p>
  <blockquote>
  <p>fatal error C1083: Cannot open include file: 'header0.h': No such file or directory</p>
  </blockquote>
  <p>to:</p>
  <blockquote>
  <p>Target contains relative path in its INTERFACE_INCLUDE_DIRECTORIES</p>
  </blockquote>
  <p>Also <code>get_target_property(LIB0_INCLUDES &quot;lib0&quot; INCLUDE_DIRECTORIES)</code> gave:</p>
  <blockquote>
  <p>get_target_property() called with non-existent target &quot;lib0&quot;.</p>
  </blockquote>