To be precise, minizip is actually a helper tool provided by zlib, located in the contrib folder of the zlib library. minizip provides a more advanced interface to manipulate files directly for compression. However, it's a bit of a pain in the ass that this tool doesn't provide a way to build with CMake. Then you can follow the way to build giflib, organize yourself, just the amount of code in this project is not too much.
Another problem is that minizip is actually an executable program, and you can't build it directly into a dynamic link library under Windows, because dynamic link libraries under Windows need to be set up to export, or else you'll be prompted with a symbol not found problem. The easiest way in this case is to organize it as a static library (Project Address), as shown below:
# Output cmake version hints
message(STATUS "The CMAKE_VERSION is ${CMAKE_VERSION}.")
# Minimum required version of cmake
cmake_minimum_required (VERSION 3.10)
# Project name, version, language
project(minizip VERSION 5.2.2)
# Support for current directory
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Determine compiler type
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
# Determine compiler type
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
message(">> using Clang")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message(">> using GCC")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
message(">> using Intel C++")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
message(">> using Visual Studio C++")
add_compile_options(/utf-8 /wd4996)
else()
message(">> unknow compiler.")
endif()
# Find a ZLIB module
find_package(ZLIB REQUIRED)
# Source code file
set(PROJECT_SOURCES )
set(PROJECT_HEADER )
# Add the source code to the executable for this project.
add_library(${PROJECT_NAME} STATIC ${PROJECT_SOURCES} ${PROJECT_HEADER})
#
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
# TODO: Add tests if needed!
# Install the header files into the include directory
install(FILES ${PROJECT_HEADER} DESTINATION include/${PROJECT_NAME})
# Install library files into lib directory
install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib # for shared libraries
ARCHIVE DESTINATION lib # for static libraries
RUNTIME DESTINATION bin # for executables
)
The key build instructions are shown below:
# Configuring CMake
cmake ... -G "$Generator" -A x64 `
-DCMAKE_CONFIGURATION_TYPES=RelWithDebInfo `
-DCMAKE_PREFIX_PATH="$InstallDir" `
-DCMAKE_INSTALL_PREFIX="$InstallDir"
# Build phase, specify build type
cmake --build . --config RelWithDebInfo
# Installation phase, specifying the build type and installation target
cmake --build . --config RelWithDebInfo --target install
Finally, we will talk about dynamic and static libraries. Dynamic libraries and static libraries have their own advantages and disadvantages, so I won't go into details here. However, I still prefer to use dynamic libraries under Windows. Binary compatibility has always been an important problem in C/C++ programming. For example, you use VS2010 compiled dynamic libraries in the VS2013 environment may not be able to use, which is still the same product of different versions will cause this binary results of the difference between the problem. However, according to the documentation provided by Microsoft (cf:/zh-cn/cpp/porting/binary-compat-2015-2017 ), VS2015 and later versions will begin to provide binary-compatible features, the principle is that the standard library, runtime libraries (such as ), C + + + standard libraries to ensure the stability of the ABI (binary interface). I also did find that many products of MSVC pre-compiled results can be mixed in the MSVC environment. For example, Qt compiled by VS2017 can be used normally in the environment of VS2019. However, these results can be mixed are generally dynamic libraries, that is, dynamic libraries are a little better binary compatibility characteristics. As for static libraries, the document claims that static libraries can also be done, but I actually test at least this VS2017-based minizip static library in VS2019 can not be used. This is left to be solved later.