2015-11-06 98 views
-1

我想学习zlib模块,但是当我编译我的代码时,它总是说zlib提供的所有函数名都没有定义。这里是我的代码,任何帮助表示感谢,谢谢。C 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 

回答

3

您需要使用zlib的链接:

cc -lz test.c -o test 

而且你的拼写是错误的。 “compressFile”与“comressFile”。

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