How to fix /usr/bin/ld: cannot find -lzlib

Problem:

You want to compile a C++ executable that uses zlib, but you see an error message like

/usr/bin/ld: cannot find -lzlib
collect2: error: ld returned 1 exit status

Solution

Use -lz instead of -lzlib. The zlib library is named libz.so, not libzlib.so!

Example:

g++ -o ztest zpipe.c -lz