lena
lena

Reputation: 727

How to fix "cannot find -lz"

I am working on code have Zlib.h header, This header is found in my code folder, I compile this code by using

gcc -o x xx.c -lz 

but I get on this

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

This happen just with Linux that I installed in a VBox. How to fix that.

Upvotes: 0

Views: 3677

Answers (2)

Mathieu
Mathieu

Reputation: 9629

When you type gcc foo.c, you ask gcc to compile and link the given file.

1. Compilation

Compilation consist of transforming the source file into an object file. This step need the included files, like zlib.h to be found by gcc.

This step seems to be correct on system.

NB: You can ask gcc to only do this step typing gcc -c foo.c, or better gcc -Wall -c foo.c

2. Link

Once the object files have be created, then need to be linked to create an executable file.

It's that step that failed for you: your linked can't find the already compiled functions needed by your code.

When linking with option -lz, you tell your linker "search for libz.so file to find the missing functions"

On current linux distribution, you can install package like libz-dev to install the .so file in well known places. (/lib, /usr/lib, /usr/local/lib...)

If you don't have the libz.so file installed on the library search path, you can specify where is the library to your linker.

For instance, if libz.so is if /bar/baz directory, you can type gcc foo.c /bar/baz/libz.so. The same for libz.a.


In any case, you'll need the libz.so file or at least the libz.a file

See also What's the difference between .so, .la and .a library files?

Upvotes: 1

Atanu Barai
Atanu Barai

Reputation: 125

Try installing 'zlib1g-dev'. On Ubuntu this following command will install the library.

sudo apt install zlib1g-dev

Upvotes: 2

Related Questions