错误1

error LNK2019: unresolved external symbol _deflateEnd@4

原因1: 未使用zlib的链接库

  • 解决:
#pragma comment(lib, "zlibstat.lib") // for static lib
#pragma comment(lib, "zdll.lib") // for dll lib

原因2:在使用静态库时即使包含了zlibstat.lib没有定义宏“ZLIB_WINAPI”

  • 解决: 在项目属性中 C/C++ -> Preprocessor -> Preprocessor Definitions 添加“ZLIB_WINAPI” 注意:这个必须在项目中添加,使用#define来添加是无效的。

错误2

error LNK2026: module unsafe for SAFESEH image

原因:两个项目SAFESEH设置不同

  • 解决: 设置项目属性: Linker -> Advanced -> [Image Has Safe Exception Handlers] = “No” 注意:这个必须在项目中添加,使用#pragma来添加是无效的。

错误3

error LNK2005: __call_reportfault already defined in LIBCMTD.lib(invarg.obj)或类似其他的error LNK2005:

原因:与libcmt库冲突了

  • 解决:
#pragma comment(linker, "/NODEFAULTLIB:libcmt.lib")

压缩

enData = zlib.compress(data)[2:-4]

对应:

compress2(dstbuf, &dstLen, strSrc, srcLen, 6);

解压

deData = zlib.decompress(enData, -zlib.MAX_WBITS)

对应:

bool gzipInflate( const std::string& compressedBytes, std::string& uncompressedBytes ) {
    if ( compressedBytes.size() == 0 ) {
        uncompressedBytes = compressedBytes ;
        return true ;
    }

    uncompressedBytes.clear() ;

    unsigned full_length = compressedBytes.size() ;
    unsigned half_length = compressedBytes.size() / 2;

    unsigned uncompLength = full_length ;
    char* uncomp = (char*) calloc( sizeof(char), uncompLength );

    z_stream strm;
    strm.next_in = (Bytef *) compressedBytes.c_str();
    strm.avail_in = compressedBytes.size() ;
    strm.total_out = 0;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;

    bool done = false ;

    //if (inflateInit2(&strm, (16+MAX_WBITS)) != Z_OK)
    if (inflateInit2(&strm, -MAX_WBITS) != Z_OK)
    {
        free( uncomp );
        return false;
    }

    while (!done) {
        // If our output buffer is too small
        if (strm.total_out >= uncompLength ) {
            // Increase size of output buffer
            char* uncomp2 = (char*) calloc( sizeof(char), uncompLength + half_length );
            memcpy( uncomp2, uncomp, uncompLength );
            uncompLength += half_length ;
            free( uncomp );
            uncomp = uncomp2 ;
        }

        strm.next_out = (Bytef *) (uncomp + strm.total_out);
        strm.avail_out = uncompLength - strm.total_out;

        // Inflate another chunk.
        int err = inflate (&strm, Z_SYNC_FLUSH);
        if (err == Z_STREAM_END) done = true;
        else if (err != Z_OK)  {
            break;
        }
    }

    if (inflateEnd (&strm) != Z_OK) {
        free( uncomp );
        return false;
    }

    for ( size_t i=0; i<strm.total_out; ++i ) {
        uncompressedBytes += uncomp[ i ];
    }
    free( uncomp );
    return true ;
}

Linux使用gcc编译使用zlib库的代码时,使用 -lz 来链接(link) zlib 库。

否则就会出现类似 undefined reference to `deflateInit_’ 的错误