How to set ZLIB_ROOT

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 :

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 :

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 :

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 :

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…

Maybe try adding OVERRIDE_FIND_PACKAGE to FetchContent_Declare of zlib.

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.

Cc: @craig.scott

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.

1 Like