8

I am trying to compile existing code provided in examples by zlib, but it is giving me error at the first place itself:

nikhil@nikhil-Vostro-3500:~/zlib-1.2.8/examples$ gcc -o zpipe -g zpipe.c 
/tmp/ccVZzqsb.o: In function `def':
/home/nikhil/zlib-1.2.8/examples/zpipe.c:32: undefined reference to `deflateInit_'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:40: undefined reference to `deflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:51: undefined reference to `deflate'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:55: undefined reference to `deflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:66: undefined reference to `deflateEnd'
/tmp/ccVZzqsb.o: In function `inf':
/home/nikhil/zlib-1.2.8/examples/zpipe.c:90: undefined reference to `inflateInit_'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:98: undefined reference to `inflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:109: undefined reference to `inflate'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:116: undefined reference to `inflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:121: undefined reference to `inflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:130: undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
nikhil@nikhil-Vostro-3500:~/zlib-1.2.8/examples$ 

How can I compile the file correctly?

kenorb
  • 155,785
  • 88
  • 678
  • 743
005at2k
  • 83
  • 1
  • 1
  • 3
  • 2
    You need to tell the compiler to link zlib in your command line. Look into the `-l` and `-L` flags in `man gcc` – OmnipotentEntity Mar 11 '14 at 22:21
  • 1
    Or possibly just run 'make' in that examples directory. And zlib1g and zlib1g-dev are the Ubuntu packages giving you zlib. – Dirk Eddelbuettel Mar 11 '14 at 22:25
  • 1
    If it's finding `zlib.h`, which it appears to be, it's reasonable to suppose that `zlib1g-dev` is installed. – Emmet Mar 11 '14 at 22:28
  • @Emmet Thanks. This is clear and helps in other problems such as "zlib.h: no such file or directory" while installing grpc from pecl or from source: https://github.com/grpc/grpc/issues/12597 – site80443 Jul 19 '21 at 22:08

1 Answers1

14

You're getting a linker error because you haven't told the compiler to link the library that contains the functions that you've used. The usual way of compiling a simple program that uses zlib on Ubuntu would be:

gcc -o foo foo.c -lz
Emmet
  • 6,192
  • 26
  • 39
  • Some clarification, the reference file isn't in tmp. The temporary object file that gcc created as a stage in the compile is in /tmp. It's a linker error. – OmnipotentEntity Mar 12 '14 at 08:15