C zlib链接错误

时间:2015-11-06 15:20:32

标签: c zlib

我正在尝试学习zlib模块,但是当我编译我的代码时,它总是说zlib提供的所有函数名都没有定义。这是我的代码,感谢任何帮助。谢谢。

#include <stdio.h>
#include <zlib.h>

int compressFile(char *infilename, char *outfilename){
        //Opens the needed files
        FILE * infile = fopen(infilename, "rb");
        gzFile outfile = gzopen(outfilename, "wb");
        //Checks if the files are correctly opened
        if (!infile || !outfile) return -1;

        char inbuffer[128];
        int numRead = 0;

        while ((numRead = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0){
                gzwrite(outfile, inbuffer, numRead);

        }
        fclose(infile);
        gzclose(outfile);

}

这是错误

cc     test.c   -o test
test.c: In function ‘main’:
test.c:24:2: warning: implicit declaration of function ‘comressFile’ [-Wimplicit-function-declaration]
  comressFile("hello.txt", "hello.zip");
  ^
/tmp/cc32e8i4.o: In function `compressFile':
test.c:(.text+0x41): undefined reference to `gzopen'
test.c:(.text+0x7c): undefined reference to `gzwrite'
test.c:(.text+0xbd): undefined reference to `gzclose'
/tmp/cc32e8i4.o: In function `main':
test.c:(.text+0xd7): undefined reference to `comressFile'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'test' failed
make: *** [test] Error 1

1 个答案:

答案 0 :(得分:3)

您需要链接zlib:

cc -lz test.c -o test

你的拼写也错了。 “compressFile”与“comressFile”。

请记住,在C 使用它们之前声明函数是可选的。如果你不这样做,或者拼写错误的名字,这个函数会在第一次使用时被隐式声明,这可能不是你想要的。