【windows】zlib-1.3.1 编译 使用

  • Zlib:从官网下载最新源码。

有cmakelist.txt,显然用cmake构建

mkdir build
cd build
cmake ..
mingw-make

编译完成后,build/ 下

mkdir build
cd build
cmake ..
mingw-make # or nmake

image

动态库libzlib.dll与动态库的链接库 libzlib.dll.a

静态库libzlibstatic.a

将目录添加到系统路径,以便查找动态库

D:\Software\zlib-1.3.1\build

示例

fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory compress.c

unistd.h是Linux下的文件,我们不需要,只要将这行删掉即可,如下图所示:

image

#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include <string.h>

#define CHUNK 1024

int main() {
    const char* data = "Hello, zlib compression!";
    uLong srcLen = strlen(data) + 1;
    uLong destLen = compressBound(srcLen);
    Bytef* dest = (Bytef*)malloc(destLen);  // 显式类型转换

    if (compress(dest, &destLen, (const Bytef*)data, srcLen) != Z_OK) return 1;
    printf("Compressed size: %lu bytes\n", destLen);

    Bytef* decompressed = (Bytef*)malloc(srcLen);  // 显式类型转换
    if (uncompress(decompressed, &srcLen, dest, destLen) != Z_OK) return 1;
    printf("Decompressed: %s\n", decompressed);

    free(dest); free(decompressed);
    return 0;
}
set(ZLIB_ROOT_DIR "D:/Software/zlib-1.3.1")
# 包含头文件
include_directories("${ZLIB_ROOT_DIR}" "${ZLIB_ROOT_DIR}/build")
# 链接动态库
target_link_libraries(CloudDriver 
	"${ZLIB_ROOT_DIR}/build/libzlib.dll.a"
)
posted @ 2025-07-10 08:09  丘狸尾  阅读(193)  评论(0)    收藏  举报