Ellis
Ellis

Reputation: 105

C zlib link errors

I am trying to learn the zlib module, however when i compile my code it always says that all the function names provided by zlib are not defined. Here is my code, any help is appreciated, thanks.

#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);

}

Here is the error

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

Upvotes: 0

Views: 247

Answers (1)

jforberg
jforberg

Reputation: 6752

You need to link with zlib:

cc -lz test.c -o test

Also your spelling is wrong. "compressFile" vs. "comressFile".

Remember, in C declaring functions before using them is optional. If you don't do it, or spell the name wrong, the function is implicitly declared at first use, which is probably not what you want.

Upvotes: 3

Related Questions