windows mingw下使用zlib

发布于 2024-10-01 13:35:58 字数 653 浏览 7 评论 0原文

我似乎无法让 zlib 在 Windows 下的 mingw 上执行任何操作。

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

简单的代码如:

#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;
}

已编译:

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

编译良好。 然而,exe 在压缩函数中崩溃了。 有什么想法吗?

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧话新听 2024-10-08 13:35:58

我建议使用 MSYS2 来处理这种事情。这些指令假定您要编译 64 位程序,但可以轻松地将它们修改为 32 位程序。

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

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

然后通过运行类似以下内容来编译代码:

gcc test.c -lz -o test

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

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

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

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.

浮萍、无处依 2024-10-08 13:35:58

查看 zlib 手册,它说:

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

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

也许 a=1024 不够大?我认为你需要调用 compressBound 来获取合适的值。

Looking at the zlib manual it says:

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.

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

意中人 2024-10-08 13:35:58

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

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

重新编译一下就可以了。

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

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

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.

Recompile and it will be working.

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

累赘 2024-10-08 13:35:58

在代码中使用 zlib 非常简单,文档(或我在 stackoverflow 上找到的各种答案)并没有明确说明这一点。

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

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

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

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

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

  5. 调用压缩或解压缩

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

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

为了让事情对我自己来说更简单,我整理了一个 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
...

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

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.

  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

That's it. It could hardly be simpler.

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

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

/** 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
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文