# How to set ZLIB\_ROOT

**URL:** <https://discourse.cmake.org/t/how-to-set-zlib-root/9765>\
**Category:** Code\
**Created:** [January 3, 2024, 4:03pm UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765 "2024-01-03T16:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [January 3, 2024, 4:03pm UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765/1 "2024-01-03T16:03:34Z")

</div>

Hello,

I am trying to install/build the ZLib library on Windows through CMake, in order to install/use libpng afterwards.

First of all, I am on Windows, and I can’t use a package manager as Conan or vcpkg.

So I tried :

```auto
FetchContent_Declare(
    zlib
    GIT_REPOSITORY https://github.com/madler/zlib.git
    GIT_TAG v1.3
    FIND_PACKAGE_ARGS NAMES ZLIB::ZLIB
)
FetchContent_MakeAvailable(zlib)

FetchContent_Declare(
    libpng
    GIT_REPOSITORY https://github.com/glennrp/libpng
    GIT_TAG 71b9b5e16ef751d973a3935284382bc344ff9941 # release 1.6.41
)
FetchContent_MakeAvailable(libpng)

```

I get several error messages, as this one :

```auto
Target "png_shared" links to:

  ZLIB::ZLIB

but the target was not found. Possible reasons include:

  * There is a typo in the target name.
  * A find_package call is missing for an IMPORTED target.
  * An ALIAS target is missing.

```

After the instructions about zlib, I added the command :  
`message("ZLib found ? ${ZLIB_FOUND}")`  
which confirmed me that ZLib was not found, _because ${ZLIB\_FOUND} doesn’t exist_.  
Note that if I use `FIND_PACKAGE_ARGS`, _${ZLIB\_FOUND} doest exist_, but its value is `false`.  
I don’t understand why there are two different behaviors…

Zlib seems to have many troubles with CMake :

> <https://github.com/madler/zlib/issues/831>
>
> @madler: It is possible to solve all problems and merge some PRs?
> 
> Thanks in a…dvance.
> 
> Issues:
> \- https://github.com/madler/zlib/issues/147
> \- https://github.com/madler/zlib/issues/259
> \- https://github.com/madler/zlib/issues/359
> \- https://github.com/madler/zlib/issues/366
> \- https://github.com/madler/zlib/issues/485
> \- https://github.com/madler/zlib/issues/497
> \- https://github.com/madler/zlib/issues/499
> \- https://github.com/madler/zlib/issues/652
> \- https://github.com/madler/zlib/issues/759
> \- https://github.com/madler/zlib/issues/762
> \- https://github.com/madler/zlib/issues/772
> \- https://github.com/madler/zlib/issues/781
> \- https://github.com/madler/zlib/issues/812
> \- https://github.com/madler/zlib/issues/849
> \- https://github.com/madler/zlib/issues/884
> \- https://github.com/zlib-ng/zlib-ng/issues/1320
> \- https://github.com/fastcws/fastcws/issues/5 + https://github.com/fastcws/fastcws/pull/6
> 
> PRs:
> \- https://github.com/madler/zlib/pull/310
> \- https://github.com/madler/zlib/pull/315
> \- https://github.com/madler/zlib/pull/337
> \- https://github.com/madler/zlib/pull/343
> \- https://github.com/madler/zlib/pull/344
> \- https://github.com/madler/zlib/pull/347
> \- https://github.com/madler/zlib/pull/395
> \- https://github.com/madler/zlib/pull/471
> \- https://github.com/madler/zlib/pull/500
> \- https://github.com/madler/zlib/pull/519
> \- https://github.com/madler/zlib/pull/526
> \- https://github.com/madler/zlib/pull/535
> \- https://github.com/madler/zlib/pull/543
> \- https://github.com/madler/zlib/pull/581
> \- https://github.com/madler/zlib/pull/677
> \- https://github.com/madler/zlib/pull/774
> \- https://github.com/madler/zlib/pull/792
> \- https://github.com/madler/zlib/pull/818
> \- https://github.com/madler/zlib/pull/824
> \- https://github.com/madler/zlib/pull/861
> \- https://github.com/madler/zlib/pull/863
> \- https://github.com/madler/zlib/pull/866
> \- https://github.com/madler/zlib/pull/878
> \- https://github.com/madler/zlib/pull/879
> 
> I have not finished:
> \- https://github.com/madler/zlib/pulls?q=is%3Apr+is%3Aopen+cmake
> \- https://github.com/madler/zlib/issues?q=is%3Aissue+is%3Aopen+cmake
> 
> Linked to:
> @Mizux, @albrematt, @johnb003, @miurahr, @WildCard65, @wrowe, @ScatteredRay, @jnhyatt, @Jihadist, @hwhsu1231, @mtl1979, @ClausKlein, @sum01, @craigscott-crascit, @hjmallon, @mattparks, @puneetmatharu, @vszakats, @tbeu, @fredgan, @RobinGeffroy, @jfern2011, @kirankotari, @etiennearnal, @omegacoleman, @yyzworker, @yalcinerbora, @niparx, @navidR, @lperron, @vguen, @tr1cks, @AndrewAtAvenza, @gvollant, @hopeless-programmer-online, @glebm, @jheaff1, @SpaceIm, @AraHaan, @pzychotic, @siepkes, @schultetwin1, @Bagira80, @wantehchang, @dankegel, @chanphil, @MichaelGoulding, @stkruse, next after.
> 
> @nmoinvaz

For now, I suppose `FindZLIB()` is called at one moment, and I see in its documentation that we can set `ZLIB_ROOT` to help it.  
Zlib has been downloaded, and put in the build directory, in a subdirectory called `_deps`.  
Then, there are three directories, alongside with three other ones related to libpng. The three directories are :

- zlib-build
- zlib-src
- zlib-subbuild

So :

1. Am I right to think setting `ZLIB_ROOT` could help me ?
2. If so, where to put the instruction, and what value should I give to it ?

Thanks.

EDIT : I **commented** all the code above, but I kept the ZLIB build. Then I added the follwing instructions :

```auto
set(ZLIB_ROOT ${CMAKE_BINARY_DIR}/_deps/zlib-build)
message("${CMAKE_BINARY_DIR}/_deps/zlib-build")
find_package("zlib")
message("ZLib found ? ${ZLIB_FOUND}")

```

But zlib is not found yet, whereas the path seems to be good…

PS: Any other hint to help me to resolve my zlib troubles is welcome…

---

<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:** [January 4, 2024, 7:29am UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765/2 "2024-01-04T07:29:02Z")

</div>

Maybe try adding `OVERRIDE_FIND_PACKAGE` to `FetchContent_Declare` of zlib.

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [January 4, 2024, 7:59am UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765/3 "2024-01-04T07:59:20Z")

</div>

> [@fenrir](#):
>
> `FetchContent_Declare`

From the documentation :

> `OVERRIDE_FIND_PACKAGE` cannot be used when `FIND_PACKAGE_ARGS` is given.

So I replace the latter by the former, and I got the same error.

---

<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:** [January 11, 2024, 1:23pm UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765/4 "2024-01-11T13:23:40Z")

</div>

Cc: @craig.scott

---

<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:** [January 12, 2024, 8:27am UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765/5 "2024-01-12T08:27:18Z")

</div>

> [@Oodini](#):
>
> ` FIND_PACKAGE_ARGS NAMES ZLIB::ZLIB`

If you want this to try using the CMake-provided `FindZLIB` module first, that should be just `FIND_PACKAGE_ARGS NAMES ZLIB`. The capitalisation of `ZLIB` in this line also matters because it needs to match up with the capitalisation of `FindZLIB`.

I will say that trying to get any sort of CMake-related changes into the official zlib repository has been a fruitless task. Many have tried, but the maintainer has shown essentially zero interest. Others have tried setting up a better maintained fork, but I haven’t seen any that have gained widespread adoption. This is a really unfortunate situation, given that zlib is such a widely used, foundational dependency of so many other things. This means trying to bring it in using FetchContent is usually a frustrating experience. You will likely end up having to set up your own fork with the patches you need to get things to work. Normally I’d say just use a package manager and be done with it, but you’ve stated up front that isn’t an option for you.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 30, 2025, 4:33pm UTC](https://discourse.cmake.org/t/how-to-set-zlib-root/9765/6 "2025-09-30T16:33:12Z")

</div>

> I am trying to install/build the ZLib library on Windows through CMake, in order to install/use libpng afterwards […] and I can’t use a package manager as Conan or vcpkg

Ha, I was recently helping someone on a different forum with a similar problem/requirements, and I remembered about this thread. They also needed to resolve `png` library in their project (_and through that also `zlib`, as it’s a dependency of `png`_), and they also did not want to use a package manager.

So I made this [example project](https://github.com/retifrav/MREs/tree/93ac2c86efa7c8815ccde3e312922c506b8a0c0a/cyberforum-ru/3213759), which can be built in two ways (_the README contains build instructions for both_):

1. With vcpkg, just in case they will reconsider in future;
2. With FetchContent, so the same way you tried to.

The FetchContent way was indeed problematic, but that is mainly “_thanks to_” `zlib` project “_specifics_”, as it was already mentioned in the thread. I could only make it work after quite some [patching](https://github.com/retifrav/MREs/blob/93ac2c86efa7c8815ccde3e312922c506b8a0c0a/cyberforum-ru/3213759/3rd-party/zlib/001-single-target-and-installation.patch) of the original `zlib` project.
