2015-11-06 100 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 之前聲明函數是可選的。如果你不這麼做,或者拼錯了名字,這個函數會在第一次使用時被隱式聲明,這可能不是你想要的。