2

I built my version of zlib (and by default it builds both static and dynamic) but seemingly it can't find the static one because it's being "zs.lib" and not in the preferred list so it can't find the static but rather the dynamic one.

My CMakeLists.txt (stripped down):

cmake_minimum_required(VERSION 3.10)
set(ZLIB_USE_STATIC_LIBS ON)
find_package(ZLIB REQUIRED)
message(STATUS "ZLIB library: ${ZLIB_LIBRARIES}")
message(STATUS "ZLIB include: ${ZLIB_INCLUDE_DIRS}")

Output (stripped):

1> [CMake] -- ZLIB library: C:/Program Files/zlib/lib/z.lib
1> [CMake] -- ZLIB include: C:/Program Files/zlib/include

which isn't my expected ("zs.lib" which is the static).

What's the workarounds for linking with static version of zlib?

1 Answer 1

1

The FindZLIB.cmake don't even include the static version library on Windows (source) and I created my PR for it.

To use it in your program properly for Windows: use the config, not the builtin FindZLIB.cmake (how to use them):

find_package(ZLIB CONFIG)

[...]

in your CMakeLists.txt, however it is advised to specify what you really want via:

find_package(ZLIB CONFIG COMPONENTS shared static REQUIRED)

It works in Linux since it annotates ".a" for static libs and ".so" for dynamic libs, but in Windows it's plain ".a"/".dll.a" (same for MinGW) or ".lib" (MSVC).

EDIT: It had been merged here:) (in the upcoming CMake 4.2.0)

Sign up to request clarification or add additional context in comments.

1 Comment

Note, that for finding ZLIB the developers recommend to use config script shipped with zlib installation instead of find script shipped with CMake: find_package(ZLIB CONFIG). See github.com/madler/zlib/blob/develop/…. This is noted in the response to your comment: github.com/madler/zlib/commit/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.