简体   繁体   English

如何在 DevC++ 中访问 Zlib.h?

[英]How to access Zlib.h in DevC++?

My teacher asked to run the following code我的老师要求运行以下代码

#include <stdio.h>
#include <string.h> // for strlen #include <assert.h>
#include "zlib.h"
int main(int argc, char* argv[])
{

    // original string len = 36
    char a[50] = "Hello Hello Hello Hello Hello Hello!";
    // placeholder for the compressed (deflated) version of "a"
    char b[50];
    // placeholder for the UNcompressed (inflated) version of "b"
    char c[50];
    printf("Uncompressed size is: %lu\n", strlen(a));
    printf("Uncompressed string is: %s\n", a);
    printf("\n  \n\n");
    // STEP 1.

    // deflate a into b. (that is, compress a into b)
    // zlib struct z_stream defstream;
    defstream.zalloc = Z_NULL;
    defstream.zfree = Z_NULL;
    defstream.opaque = Z_NULL;

    // setup "a" as the input and "b" as the compressed output

    defstream.avail_in = (uInt)strlen(a) + 1; // size of input, string + terminator
    defstream.next_in = (Bytef*)a; // input char array
    defstream.avail_out = (uInt)sizeof(b); // size of output
    defstream.next_out = (Bytef*)b; // output char array

    // the actual compression work.

    deflateInit(&defstream, Z_BEST_COMPRESSION);
    deflate(&defstream, Z_FINISH);
    deflateEnd(&defstream);
    // This is one way of getting the size of the output

    printf("Compressed size is: %lu\n", strlen(b));
    printf("Compressed string is: %s\n", b);

    printf("\n  \n\n");

    // STEP 2.

    // inflate b into c

    // zlib struct z_stream infstream;
    infstream.zalloc = Z_NULL;
    infstream.zfree = Z_NULL;
    infstream.opaque = Z_NULL;

    // setup "b" as the input and "c" as the compressed output

    infstream.avail_in = (uInt)((char*)defstream.next_out - b); // size of input

    infstream.next_in = (Bytef*)b; // input char array

    infstream.avail_out = (uInt)sizeof(c); // size of output

    infstream.next_out = (Bytef*)c; // output char array

    // the actual DE-compression work. inflateInit(&infstream);
    inflate(&infstream, Z_NO_FLUSH);
    inflateEnd(&infstream);
    printf("Uncompressed size is: %lu\n", strlen(c));
    printf("Uncompressed string is: %s\n", c);
    // make sure uncompressed is exactly equal to original. assert(strcmp(a,c)==0);
    return 0;
}

I'm using Dev C++ compiler and I'm beginner to using external header files.我正在使用 Dev C++ 编译器,我是使用外部 header 文件的初学者。 How do I add zlib in devc++?如何在 devc++ 中添加 zlib? Or please suggest other ways to execute the program.或者请提出其他执行程序的方法。

Note: I using Windows OS注意:我使用 Windows 操作系统

If the file is not installed, you have to first download it from here: https://zlib.net/如果该文件没有安装,你必须先从这里下载它: https://zlib.net/

Once you install it and it is on your include path, you can include it:一旦你安装它并且它在你的包含路径上,你可以包含它:

#include <zlib.h>

Alternatively, you can move it to same directory as your main.c file, then your code should compile.或者,您可以将其移动到与 main.c 文件相同的目录,然后您的代码应该可以编译。 Make sure you move all the required files to the directory, normally the.h file only includes declarations, so you'd need a.c file too.确保将所有必需的文件移动到目录中,通常 .h 文件仅包含声明,因此您还需要一个 .c 文件。

If you do it this way, you won't need to add any other files.如果您这样做,则无需添加任何其他文件。 I'd advise against using DevC++, is an old, abandoned IDE that uses a non-standard compiler, if you want to use an IDE, I recommend CodeBlocks, a free and open multi platform IDE.我建议不要使用 DevC++,它是一个旧的、废弃的 IDE,它使用非标准编译器,如果你想使用 IDE,我推荐 CodeBlocks,一个免费和开放的多平台 Z581D6381F35E4B36D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM