windows mingw下使用zlib
我似乎无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用 MSYS2 来处理这种事情。这些指令假定您要编译 64 位程序,但可以轻松地将它们修改为 32 位程序。
安装 MSYS2 后,运行“开始”菜单中的“MinGW-w64 Win64 Shell”快捷方式。通过运行以下命令来安装 64 位工具链:
然后通过运行类似以下内容来编译代码:
我没有仔细检查您的代码,但我能够运行您的代码而不会发生任何崩溃,因此您的代码可能没问题。您的代码也没有给出任何输出,因此很难判断它是否真的有效。
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:
Then compile your code by running something like this:
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.
查看 zlib 手册,它说:
也许
a=1024
不够大?我认为你需要调用 compressBound 来获取合适的值。Looking at the zlib manual it says:
Maybe
a=1024
isn't big enough? I think you need to callcompressBound
to get a suitable value.我尝试使用 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.
在代码中使用 zlib 非常简单,文档(或我在 stackoverflow 上找到的各种答案)并没有明确说明这一点。
以下技术适用于任何编译器和 IDE。我使用 code:blocks 在 windows mingw 中测试了它,这就是为什么我将其发布为这个问题的答案。
从http://www.zlib.net/下载zlib源代码
复制所有 .将 zlib 源的根文件夹中的 c 和 .h 文件复制到编译器搜索路径中的文件夹。
将 zlib 源文件添加到 IDE 项目。
将#include“zlib.h”添加到您的源代码中
调用压缩或解压缩
就是这样。简直再简单不过了。
您需要注意的是内存管理,因为这是 C 代码。
为了让事情对我自己来说更简单,我整理了一个 C++ 包装器,欢迎您使用,如下所示:
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.
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 folder in your compiler search path.
Add the zlib source files to the IDE project.
Add #include "zlib.h" to your source code
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: