title: How to add zlib as an external project as a dependency for another external project in CMake?
body: <p>I have a project where I write a library that uses the <a href="https://github.com/Exiv2/exiv2" rel="nofollow noreferrer">exiv2</a> library. <code>exiv2</code> itself needs <a href="https://github.com/madler/zlib" rel="nofollow noreferrer">zlib</a>. If I don't import <code>zlib</code> as an external project, I get it to work by installing the package <code>zlib1g-dev</code> (on Ubuntu), because then <code>find_package( ZLIB REQUIRED )</code> from <code>exiv2</code> will find it.</p>
  <p>For reasons of cross-compilation, I cannot use the pre-compiled library from <code>zlib1g-dev</code>. That's why I want to add the <code>zlib</code> source as an external project and a dependency for <code>exiv2</code> (also an external project). This is how far I got:</p>
  <p><code>CMakeLists.txt</code> of my library:</p>
  <pre><code># Download and unpack zlib at configure time
  configure_file(CMakeLists.in.zlib zlib-download/CMakeLists.txt)
  execute_process(COMMAND &quot;${CMAKE_COMMAND}&quot; -G &quot;${CMAKE_GENERATOR}&quot; .
      WORKING_DIRECTORY &quot;${CMAKE_CURRENT_BINARY_DIR}/zlib-download&quot;
  )
  execute_process(COMMAND &quot;${CMAKE_COMMAND}&quot; --build .
      WORKING_DIRECTORY &quot;${CMAKE_CURRENT_BINARY_DIR}/zlib-download&quot;
  )
  # Add zlib to the build
  add_subdirectory(&quot;${CMAKE_CURRENT_BINARY_DIR}/zlib-src&quot;
                   &quot;${CMAKE_CURRENT_BINARY_DIR}/zlib-build&quot;
  )
  
  
  # Download and unpack exiv2 at configure time
  configure_file(CMakeLists.in.exiv2 exiv2-download/CMakeLists.txt)
  execute_process(COMMAND &quot;${CMAKE_COMMAND}&quot; -G &quot;${CMAKE_GENERATOR}&quot; .
      WORKING_DIRECTORY &quot;${CMAKE_CURRENT_BINARY_DIR}/exiv2-download&quot;
  )
  execute_process(COMMAND &quot;${CMAKE_COMMAND}&quot; --build .
      WORKING_DIRECTORY &quot;${CMAKE_CURRENT_BINARY_DIR}/exiv2-download&quot;
  )
  # Override options of exiv2
  set(EXIV2_BUILD_SAMPLES OFF CACHE BOOL &quot;&quot; FORCE)
  set(EXIV2_BUILD_EXIV2_COMMAND OFF CACHE BOOL &quot;&quot; FORCE)
  set(BUILD_SHARED_LIBS OFF CACHE BOOL &quot;&quot; FORCE)
  set(EXIV2_ENABLE_DYNAMIC_RUNTIME OFF CACHE BOOL &quot;&quot; FORCE)
  # Add exiv2 to the build
  add_subdirectory(&quot;${CMAKE_CURRENT_BINARY_DIR}/exiv2-src&quot;
                   &quot;${CMAKE_CURRENT_BINARY_DIR}/exiv2-build&quot;
  )
  
  add_library(mylibrary mylibrary.cpp)
  target_link_libraries(mylibrary exiv2lib)
  </code></pre>
  <p>Where <code>CMakeLists.in.zlib</code> is:</p>
  <pre><code>cmake_minimum_required(VERSION 2.8.2)
  
  project(zlib-download NONE)
  
  include(ExternalProject)
  ExternalProject_Add(zlib
    GIT_REPOSITORY    https://github.com/madler/zlib.git
    GIT_TAG           master
    SOURCE_DIR        &quot;${CMAKE_CURRENT_BINARY_DIR}/zlib-src&quot;
    BINARY_DIR        &quot;${CMAKE_CURRENT_BINARY_DIR}/zlib-build&quot;
    CONFIGURE_COMMAND &quot;&quot;
    BUILD_COMMAND     &quot;&quot;
    INSTALL_COMMAND   &quot;&quot;
    TEST_COMMAND      &quot;&quot;
  )
  </code></pre>
  <p>And <code>CMakeLists.in.exiv2</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 2.8.2)
  
  project(exiv2-download NONE)
  
  include(ExternalProject)
  ExternalProject_Add(exiv2
    GIT_REPOSITORY    https://github.com/Exiv2/exiv2.git
    GIT_TAG           master
    SOURCE_DIR        &quot;${CMAKE_CURRENT_BINARY_DIR}/exiv2-src&quot;
    BINARY_DIR        &quot;${CMAKE_CURRENT_BINARY_DIR}/exiv2-build&quot;
    CONFIGURE_COMMAND &quot;&quot;
    BUILD_COMMAND     &quot;&quot;
    INSTALL_COMMAND   &quot;&quot;
    TEST_COMMAND      &quot;&quot;
  )
  </code></pre>
  <p>But using these CMake files returns an error:</p>
  <pre class="lang-none prettyprint-override"><code>CMake Error at /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
    Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
  Call Stack (most recent call first):
    /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
    /usr/share/cmake-3.10/Modules/FindZLIB.cmake:112 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
    /tmp/QtCreator-wQKSzo/qtc-cmake-XXbL8ZLF/mylibrary/exiv2-src/cmake/findDependencies.cmake:24 (find_package)
    /tmp/QtCreator-wQKSzo/qtc-cmake-XXbL8ZLF/mylibrary/exiv2-src/CMakeLists.txt:62 (include)
  </code></pre>
  <p>So, how can I make <code>zlib</code> available to <code>exiv2</code>?</p>