# How do I integrate zlib with my build?

**URL:** <https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658>\
**Category:** Code\
**Created:** [December 14, 2021, 2:00pm UTC](https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658 "2021-12-14T14:00:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrZoraman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrzoraman/32/2033_2.png) [@MrZoraman](https://discourse.cmake.org/u/MrZoraman)\
**Post date:** [December 14, 2021, 2:00pm UTC](https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658/1 "2021-12-14T14:00:04Z")

</div>

zlib is built with cmake, and if I `add_subdirectory` it, it generates a required header `zconf.h` which it puts in the _binary_ dir. Other projects that depend on zlib like libpng ask for the include dir of zlib (`Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)`), but what am I supposed to give it if zlib’s headers are split across its source dir _and_ binary dir?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [December 14, 2021, 4:30pm UTC](https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658/2 "2021-12-14T16:30:09Z")

</div>

Using `add_subdirectory` for a dependency means that `find_package()` for that dependency should not be done. I think you may want to investigate the `FetchContent` module instead.

Cc: @craig.scott

---

<div class="post-metadata">

**Author:** ![MrZoraman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrzoraman/32/2033_2.png) [@MrZoraman](https://discourse.cmake.org/u/MrZoraman)\
**Post date:** [December 15, 2021, 2:46am UTC](https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658/3 "2021-12-15T02:46:10Z")

</div>

I’m using the FetchContent module. As far as I know from reading the docs though, FetchContent just does `add_subdirectory`.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [December 15, 2021, 8:20am UTC](https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658/4 "2021-12-15T08:20:00Z")

</div>

If you need to pass it around and have some other modules that depend on it, then quite possibly a superbuild with `ExternalProject` + `find_package` would be better for you.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [December 15, 2021, 8:29pm UTC](https://discourse.cmake.org/t/how-do-i-integrate-zlib-with-my-build/4658/5 "2021-12-15T20:29:41Z")

</div>

Mixing FetchContent with `find_package()` for the same dependency isn’t really a supported scenario right now. The change in [MR 5688](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5688) would provide that capability, but it has been put on hold for now (the [associated issue](https://gitlab.kitware.com/cmake/cmake/-/issues/21687) has a much longer discussion).
