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.

1 Like

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

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, 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 tozlib project “specifics”, as it was already mentioned in the thread. I could only make it work after quite some patching of the original zlib project.