Skip to content

static link minizip-ng and zlib-ng failed on Windows #900

@Chirimen-Jako

Description

@Chirimen-Jako

Hello,
I'm trying to create a Windows x64 MSVC .exe application that statically links minizip-ng and zlib-ng.
I generated a *.vcxproj file using the following commands:

rmdir /s /q third_party
rmdir /s /q build
cmake -S . -B build --fresh -DMZ_BUILD_TESTS=ON -DMZ_COMPRESS_ONLY=ON -DMZ_BZIP2=OFF -DMZ_LZMA=OFF -DMZ_ZSTD=OFF -DMZ_PKCRYPT=OFF -DMZ_WZAES=OFF -Wno-dev

Then, when I try to build the ZERO_CHECK, zlibstatic, and minizip libraries in Visual Studio 2022, I get a LINK error.
The minizip library project's References list includes zlib, not zlibstatic.
As a simple fix, I manually removed the reference to zlib and added a reference to zlibstatic, and the build was successful.
After examining minizip-ng's CMakeLists.txt, I discovered that if ZLIB_COMPAT is set to OFF in the following line:

        # Have to add zlib to install targets
        if(ZLIB_COMPAT AND (NOT DEFINED BUILD_SHARED_LIBS OR NOT BUILD_SHARED_LIBS))
            list(APPEND MINIZIP_DEP zlibstatic)
        else()
            list(APPEND MINIZIP_DEP zlib)
        endif()

If ZLIB_COMPAT is set to OFF, the shared library name will be used even if BUILD_SHARED_LIBS is undefined.
The git log shows a June 2, 2024, fix with the commit message 'Fixed zlib-ng build error about installing zlibstatic alias.' (db940ff). This suggests that the if statement's reference to the ZLIB_COMPAT variable for static link creation is a meaningful insertion.

I also discovered that ZLIB_COMPAT here is initialized to OFF by an option in the zlib-ng CMakeLists.txt, which is called by the immediately preceding add_subdirectory(${ZLIB_SOURCE_DIR} ${ZLIB_BINARY_DIR} EXCLUDE_FROM_ALL) .
I'm not sure whether this code is an intentional mechanism that cleverly uses CMake options and scopes.
I wanted to separate the static/shared library building from the ZLIB_COMPAT setting, so I temporarily swapped the order of the if() statements and removed ZLIB_COMPAT from them, and it appears to build as expected.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 213d03a..16fa287 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -207,19 +207,22 @@ if(MZ_ZLIB)
         list(APPEND MINIZIP_INC ${ZLIB_SOURCE_DIR})
         list(APPEND MINIZIP_INC ${ZLIB_BINARY_DIR})

-        # Have to add zlib to install targets
-        if(ZLIB_COMPAT AND (NOT DEFINED BUILD_SHARED_LIBS OR NOT BUILD_SHARED_LIBS))
-            list(APPEND MINIZIP_DEP zlibstatic)
-        else()
-            list(APPEND MINIZIP_DEP zlib)
-        endif()
-
         if(EXISTS "${ZLIB_BINARY_DIR}/zlib-ng.h")
             message(STATUS "ZLIB repository detected as ZLIBNG")
             set(ZLIB_COMPAT OFF)
         else()
             set(ZLIB_COMPAT ON)
         endif()
+
+        # Have to add zlib to install targets
+        if(NOT DEFINED BUILD_SHARED_LIBS OR NOT BUILD_SHARED_LIBS)
+            list(APPEND MINIZIP_DEP zlibstatic)
+        else()
+            list(APPEND MINIZIP_DEP zlib)
+        endif()
+
     else()
         message(STATUS "ZLIB library not found")

Will this approach cause problems in other environments?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions