windows mingw下使用zlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4163170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:36:07  来源:igfitidea点击:

Using zlib under windows mingw

cwindowsmingwzlib

提问by myforwik

I can't seem to get zlib to do anything on mingw under windows.

我似乎无法让 zlib 在 windows 下的 mingw 上做任何事情。

I downloaded zlib @ http://sourceforge.net/projects/mingw/files_beta/MinGW/zlib/zlib-1.2.3-1-mingw32/and put the header and lib files in the right place.

我下载了 zlib @ http://sourceforge.net/projects/mingw/files_beta/MinGW/zlib/zlib-1.2.3-1-mingw32/并将头文件和 lib 文件放在正确的位置。

Simple code like:

简单的代码如:

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

#include "zlib.h"

int main(int argc, char *argv[])
{
    long a;
    char buffer[1024];
    a = 1024;
    compress(buffer,&a,"testing",7);
    return 0;
}

compiled:

编译:

gcc test.c -lzlib -Wall -o test.exe

Compiles fine. However the exe crashes at the compress function. Any ideas?

编译得很好。但是 exe 在 compress 函数时崩溃。有任何想法吗?

回答by David Grayson

I recommend using MSYS2for this kind of thing. These instructions assume you want to compile a 64-bit program, but they can easily be modified for 32-bit.

我建议将MSYS2用于这种事情。这些说明假设您要编译 64 位程序,但可以轻松地将它们修改为 32 位。

After installing MSYS2, run the "MinGW-w64 Win64 Shell" shortcut in your Start Menu. Install the 64-bit toolchain by running:

安装 MSYS2 后,在开始菜单中运行“MinGW-w64 Win64 Shell”快捷方式。运行以下命令安装 64 位工具链:

pacman -S mingw-w64-x86_64-toolchain mingw-w64-x86_64-zlib

Then compile your code by running something like this:

然后通过运行这样的东西来编译你的代码:

gcc test.c -lz -o test

I did not check your code carefully, but I was able to run your code without any crashing, so your code might be OK. Your code also gives no output so it's hard to tell if it really worked.

我没有仔细检查您的代码,但我能够运行您的代码而不会发生任何崩溃,因此您的代码可能没问题。您的代码也没有输出,因此很难判断它是否真的有效。

回答by David Grayson

Looking at the zlib manual it says:

查看 zlib 手册,它说:

ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
                           const Bytef *source, uLong sourceLen));

Compresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least the value returned by compressBound(sourceLen). Upon exit, destLen is the actual size of the compressed buffer.

ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
                           const Bytef *source, uLong sourceLen));

将源缓冲区压缩到目标缓冲区。sourceLen 是源缓冲区的字节长度。输入时,destLen 是目标缓冲区的总大小,它必须至少是 compressBound(sourceLen) 返回的值。退出时,destLen 是压缩缓冲区的实际大小。

Maybe a=1024isn't big enough? I think you need to call compressBoundto get a suitable value.

可能a=1024不够大?我认为您需要致电compressBound以获得合适的价值。

回答by jlguenego

I tried to use the zlib from MSYS (accessible with mingw-get) and got the same problem as described below.

我尝试使用 MSYS 中的 zlib(可通过 mingw-get 访问)并遇到了如下所述的相同问题。

The solution is to do a static link instead of using the shared library. Just remove or rename the import library libz.dll.a to avoid the linker to do a link with the msys-z.dll.

解决办法是做一个静态链接,而不是使用共享库。只需删除或重命名导入库 libz.dll.a 即可避免链接器与 msys-z.dll 进行链接。

Recompile and it will be working.

重新编译,它将工作。

Another way is to install zlib yourself from the zlib.net website. Remove the one from mingw-get.

另一种方法是自己从 zlib.net 网站安装 zlib。从 mingw-get 中删除一个。

回答by ravenspoint

Using zlib in your code is extremely simple, something that the documentation ( or the various answers on stackoverflow I found ) don't make obvious.

在您的代码中使用 zlib 非常简单,文档(或我发现的关于 stackoverflow 的各种答案)没有明确说明。

The following technique works for any compiler and IDE. I tested it in windows mingw using code:blocks, which is why I am posting it as an answer to this question.

以下技术适用于任何编译器和 IDE。我使用 code:blocks 在 windows mingw 中对其进行了测试,这就是我将其发布为该问题的答案的原因。

  1. Download the zlib source code from http://www.zlib.net/

  2. Copy all the .c and .h files from the root folder of the zlib source to a folder in your compiler search path.

  3. Add the zlib source files to the IDE project.

  4. Add #include "zlib.h" to your source code

  5. Call compress or uncompress

  1. http://www.zlib.net/下载 zlib 源代码

  2. 将所有 .c 和 .h 文件从 zlib 源的根文件夹复制到编译器搜索路径中的文件夹。

  3. 将 zlib 源文件添加到 IDE 项目中。

  4. 将 #include "zlib.h" 添加到您的源代码中

  5. 调用压缩或解压缩

That's it. It could hardly be simpler.

就是这样。再简单不过了。

All you have to be careful about is memory management, since this is c code.

您需要注意的是内存管理,因为这是 c 代码。

To make things even simpler for myself, I have put together a c++ wrapper which you are welcome to use, like this:

为了让我自己更简单,我整理了一个 C++ 包装器,欢迎您使用,如下所示:

/** ZLIB C++ wrapper

Usage:

<pre>

    #include "cZLIB.h"

    {
    // compress data in bigbuffer
    raven::set::cZLIB ZLIB;
    ZLIB.Compress( bigbuffer, sizebigbuffer );

    // use compressed buffer, before ZLIB goes out of scope
    use( ZLIB.Buffer(), ZLIB.Length() );

    }

    ...

    {

    // decompress data in smallbuffer
    raven::set::cZLIB ZLIB;
    ZLIB.Inflate( smallbuffer, sizesmallbuffer )

    // use decompressed data, before ZLIB goes out of scope
    use( ZLIB.Buffer(), ZLIB.Length() );

    }

</pre>

Build:

Download this code ( cZLIB.h and cZLIB.cpp ) from

https://github.com/JamesBremner/raven-set

and install somewhere in your compiler search path.
Let's assume you install it in folder .../src.

Download the zlib source code from http://www.zlib.net/
Copy all the .c and .h files from the root folder of the zlib source
to a new folder .../src/zlib

Add the files cZLIB.h, cZLIB.cpp and all the files in .../src/zlib
to the IDE project.

Build.

 */
class cZLIB
...