title: CMake: Using add_subproject with a library using Include ends up in wrong relative path
body: <h1>Global CMakeLists.txt</h1>
  
  <p>This CMakeLists.txt compiles a few C++ files (Qt5/...) with MOC and is not 'special' in any regards.</p>
  
  <p>The code in question is this:</p>
  
  <pre><code>add_subdirectory(third-party/libwebrtc)
  include_directories(third-party/libwebrtc)
  target_link_libraries(${PROJECT_NAME} libwebrtc)
  </code></pre>
  
  <h1>libwebrtc CMakeLists.txt</h1>
  
  <pre><code>cmake_minimum_required(VERSION 3.3)
  project(libwebrtc)
  
  # Allow the use of IN_LIST operand
  cmake_policy(SET CMP0057 NEW)
  
  set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
      ${CMAKE_SOURCE_DIR}/CMakeModules)
  
  find_package(Git REQUIRED)
  
  include(FindLibraries)
  include(Version)
  include(Options)
  include(TargetOsAndCpu)
  ...
  </code></pre>
  
  <p>Full libwebrtc source -> <a href="https://github.com/cloudwebrtc/libwebrtc-build/blob/dev/CMakeLists.txt" rel="nofollow noreferrer">https://github.com/cloudwebrtc/libwebrtc-build/blob/dev/CMakeLists.txt</a></p>
  
  <h1>Error on 'cmake ..'</h1>
  
  <p>If I run <code>mkdir build; cd build; cmake ..</code> I can see that CMake searches the <code>include()</code> in the top-level project and not in its subdirectory as I had expected it. </p>
  
  <pre><code>joachim@thinkpad-x1:[~/projects/client/build]: cmake ..
  -- CMAKE_SYSTEM_INFO_FILE: 
  -- CMAKE_SYSTEM_NAME:      
  -- CMAKE_SYSTEM_PROCESSOR: 
  -- CMAKE_SYSTEM:           
  -- CMAKE_C_COMPILER:       /usr/bin/clang
  -- CMAKE_CXX_COMPILER:     /usr/bin/clang++
  CMake Error at sources/third-party/libwebrtc/CMakeLists.txt:13 (include):
    include could not find load file:
  
      FindLibraries
  
  
  CMake Error at sources/third-party/libwebrtc/CMakeLists.txt:14 (include):
    include could not find load file:
  
      Version
  </code></pre>
  
  <h1>Question (original)</h1>
  
  <p>Is there something I can do about that other than integrating the library into my base-project by copying the includes from the CMakeModules directory?</p>
  
  <p>Thanks this has been answered below, still leaving this here.</p>
  
  <h1>Question about dependencies</h1>
  
  <p>The add_subdirectory call builds the libwebrtc correctly, but my include_directories call, the one from the top level CMakeLists.txt (not the libwebrtc library), wants to include files which are only available after the complete build:</p>
  
  <pre><code>include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}/sources/third-party/libwebrtc/include/webrtc
    ${CMAKE_CURRENT_BINARY_DIR}/sources/third-party/libwebrtc/include/webrtc/third_party/libyuv/include/
    ${CMAKE_CURRENT_BINARY_DIR}/sources/third-party/libwebrtc/webrtc/src/third_party/abseil-cpp
  )
  </code></pre>
  
  <p>How to make cmake depend on the complete build of the library and then build the main program onwards? Usually one uses <strong>add_custom_command(..)</strong> but since we are using <strong>add_subdirectory(..)</strong> we can't also use <strong>add_custom_command(..)</strong>.</p>
  
  <p>My hack to this problem was to rename the library to project(libwebrtcx) inside libwebrtc/CMakeLists.txt and also the ExternalProject_Add(libwebrtcx ...) and add a <strong>add_dependencies(${PROJECT_NAME} libwebrtcx)</strong> but can it be done  without that hack?</p>