209

I am trying to compile Android source code under Ubuntu 10.04. I get an error saying,

/usr/bin/ld: cannot find -lz

Can you please tell me how can I fix it? What does cannot find -lz mean? Here's the full error message:

external/qemu/Makefile.android:1101: warning: overriding commands for target `external/qemu/android/avd/hw-config-defs.h'
external/qemu/Makefile.android:933: warning: ignoring old commands for target `external/qemu/android/avd/hw-config-defs.h'
host SharedLib: libneo_cgi (out/host/linux-x86/obj/lib/libneo_cgi.so)
/usr/bin/ld: skipping incompatible /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../libz.so when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../libz.a when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/libz.so when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/libz.a when searching for -lz
/usr/bin/ld: cannot find -lz
collect2: ld returned 1 exit status
make: *** [out/host/linux-x86/obj/lib/libneo_cgi.so] Error 1

And my GCC version output:

scheung@scheung-virtual-box:/media/EXTDIV/mydroid$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I already have the zlib1g-dev library installed:

$ sudo apt-get install zlib1g-dev 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
zlib1g-dev is already the newest version.

(I get that from this link.)

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
michael
  • 106,540
  • 116
  • 246
  • 346

11 Answers11

185

I had the exact same error, and like you, installing zlib1g-dev did not fix it. Installing lib32z1-dev got me past it. I have a 64 bit system and it seems like it wanted the 32 bit library.

kylefinn
  • 2,398
  • 1
  • 14
  • 12
  • 27
    It worked here with libz-dev (virtual package for zlib1g-dev) on Kubuntu 12.04 x64. – Qsiris Jan 22 '13 at 16:06
  • 10
    Also helped me install/build lxml-3.4.0 for Python via pip on Ubuntu 14.04 LTS/trusty. Thanks! – Marian Sep 15 '14 at 08:07
171

For x64 install zlib1g-dev.

sudo apt-get install zlib1g-dev

I don't need all the x86 libs ;)

phedoreanu
  • 2,588
  • 3
  • 25
  • 29
59

sudo apt-get install libz-dev in ubuntu.

Ravimallya
  • 6,550
  • 2
  • 41
  • 75
Siva mbigai M
  • 601
  • 5
  • 3
  • 2
    Could somebody shed some light into why is this necessary? I was able to install python-lxml in a virtual environment ONLY after installing this package. However if I tried to install it globally, I wouldn't have any problem at all. :\ – cballenar Jul 22 '16 at 15:02
  • Not certain about that case specifically, but broadly `python-lxml` requires logic contained in the zlib library which it cannot or simply doesn't provide itself. If it cannot reach the library, either because it's missing or some permissions barrier, it will be unable to build/function. – ti7 Feb 07 '20 at 22:00
  • 1
    @cballenar : Short answer: **_Dependencies_!** `python-lxml` depends on any package that `Provides: libz-dev`. C/C++ programs are compiled and linked against libraries which provide functions, routines, & data structures. Unix systems use `.so`, `.a`, `.la` files & Windows use `.dll`. These are compiled shared library files, that contain routines that other programs can link against and use to implement some functionality. For this example: `libz` implements [data compression & decompression](https://zlib.net/) functions. – TrinitronX Jul 05 '20 at 00:08
  • @cballenar : Long answer / details (_part 1_): `libz-dev` is a [virtual package](https://www.debian.org/doc/debian-policy/ch-relationships.html#s-virtual) on Ubuntu (verify this on your system with: `apt show libz-dev`). The other package `zlib1g-dev` provides `libz-dev`, which includes the shared library file `/usr/lib/x86_64-linux-gnu/libz.a`, and the [C Header files](https://www.tutorialspoint.com/cprogramming/c_header_files.htm) `/usr/include/zconf.h` & `/usr/include/zlib.h`. You can view the package contents with: `dpkg -L zlib1g-dev`, which shows all the files it contains. – TrinitronX Jul 05 '20 at 00:15
  • @cballenar : Long answer (_part 2_): As mentioned, [`libz-dev`](https://packages.ubuntu.com/bionic/zlib1g-dev) ([also in Debian](https://packages.debian.org/buster/zlib1g-dev)) is [provided](https://pkgs.org/search/?q=libz-dev&on=provides) by [`zlib1g-dev`](https://pkgs.org/search/?q=zlib1g-dev) via the Debian Package `Provides: libz-dev` line in the [Debian source package's](https://packages.ubuntu.com/source/bionic/zlib) `debian/control` file. This is how the `apt` packaging manager knows to resolve the `python-lxml` compile-time virtualenv package dependency on `libz-dev` via `zlib1g-dev`. – TrinitronX Jul 05 '20 at 00:30
  • @cballenar : Long answer (_part 3_): On Linux, there are [multiple types of library files](https://www.tecmint.com/understanding-shared-libraries-in-linux/). They are [mainly used with C/C++ programs](https://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/), because they are the resulting output of the compilation process. These can be: shared (a.k.a dynamic) (`. so`), static (`.a`), and libtool convenience libraries (`.la` Note: These are [not actually binary compiled libraries](https://stackoverflow.com/a/1238079/645491), but helper files that `libtool` uses internally). – TrinitronX Jul 05 '20 at 00:41
  • @cballenar : Long answer (_part 4_): These types of library files, along with [C/C++ Header files](https://www.tutorialspoint.com/cprogramming/c_header_files.htm) (`.h`) are usually contained in `*-dev` type packages on Debian/Ubuntu. The [notable exception](https://www.debian.org/doc/manuals/maint-guide/advanced.en.html#library) being the libtool [convenience library (`.la`) files](https://www.gnu.org/software/automake/manual/html_node/Libtool-Convenience-Libraries.html#Libtool-Convenience-Libraries). Debian recommends against including `.la` files due to issues with hidden dependencies. – TrinitronX Jul 05 '20 at 00:47
  • @cballenar : Long answer (_part 5_): The [Debian project has a goal to remove `.la` files from their packaging](https://wiki.debian.org/ReleaseGoals/LAFileRemoval) for this reason, but you still might come across the `.la` files in some older `*-dev` packages. So, long story short: there's a lot of complexity hiding inside these Debian packages and they try to do their best at resolving dependencies for both _**runtime**_, _**compile time**_, and _**linking time**_. The first **runtime** dependency is handled by the actual shared library `.so` file. – TrinitronX Jul 05 '20 at 00:56
  • @cballenar : Long answer (_part 6_): In your case, `libz-dev` provides the types of files that are needed at either: compile or linker time. Many _scripting language environment & version managers_ (e.g.: RVM, Virtualenv, Pyenv, etc...) actually compile and link against C/C++ libraries **_during their operations_**! This is because they manage separate versions and installations of Ruby, Python, etc... along with separate installs of any scripting language libraries (Ruby `gem`s, Python `pip` packages, ...). These are managed in other directories, separated from the base system OS files. – TrinitronX Jul 05 '20 at 01:02
  • @cballenar : Long answer (_part 7_): At some point scripting language binaries need to be **_compiled and linked_** against the base system packages that provide _compile_ and _link_ time shared libraries. This is how the Scripting language itself is able to provide functionality such as provided by `zlibg1` on LInux, or `zlib` on Windows / MacOS. In this way, the scripts are abstracted away from the base OS and platform using the scripting language. Yet, the actual binary code executed still lives in the base OS system libraries & packages. Virtualenv compiles & links during python install – TrinitronX Jul 05 '20 at 01:07
  • @cballenar : Long answer (_part 8_): We just talked about the way tools like `virtualenv`, `pyenv`, and `rvm` need to compile their scripting language binaries and link against the base Operating System shared libs. There's one last part: Finally, some Ruby `gem` & Python `pip` packages _also provide or use_ C/C++ library bindings or "native / binary extensions" which build upon the base functionality of the scripting language by using new miscellaneous system shared libraries and extending the scripting language with those new functions and capabilities. This needs compilation & linking too! – TrinitronX Jul 05 '20 at 01:11
  • @cballenar : Long answer (_part 9_): So, after unpacking all this info about C/C++, package managers, dependencies, scripting languages & their version managers, and the types of files & libraries needed at compile & linker time... Now we can finally see why the Virtualenv installation of `python-lxml` needed the `zlib1g-dev` package's `libz.a` and `zlib.h` files during _**compile**_ and _**linker**_ time. Meanwhile, the package manager's version of `python-lxml` was **pre-compiled**, and thus depends on `libxml2 => zlib1g` and _did **not**_ need this `*-dev` dependency package – TrinitronX Jul 05 '20 at 01:24
35

I just encountered this problem and contrary to the accepted solution of "your make files are broken" and "host includes should never be included in a cross compile"

The android build includes many host executables used by the SDK to build an android app. In my case the make stopped while building zipalign, which is used to optimize an apk before installing on an android device.

Installing lib32z1-dev solved my problem, under Ubuntu you can install it with the following command:

sudo apt-get install lib32z1-dev
liberforce
  • 11,189
  • 37
  • 48
Adam
  • 351
  • 3
  • 2
13

I had the exact same error, Installing zlib-devel solved my problem, Type the command and install zlib package.

On linux:

sudo apt-get install zlib*

On Centos:

sudo yum install zlib*
Abhi km
  • 159
  • 1
  • 5
7

Another possible cause: You've passed --static to the linker, but you only have a dynamic version of libz (libz.so), but not a version that can be statically linked (libz.a).

jhclark
  • 2,493
  • 1
  • 20
  • 14
7

It means you asked it to include the library 'libz.a' or 'libz.so' containing a compression package, and although the compiler found some files, none of them was suitable for the build you are using.

You either need to change your build parameters or you need to get the correct library installed or you need to specify where the correct library is on the link command line with a -L/where/it/is/lib type option.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
7

Try one of those three solution. It must work :) :

  1. sudo apt-get install zlib1g-dev
  2. sudo apt-get install libz-dev
  3. sudo apt-get install lib32z1-dev

In fact what is missing is not the lz command, but the development files for the zlib library.So you should install zlib1g-devlib for ex to get it.

For rhel7 like systems the package is zlib-devel

Gryu
  • 2,102
  • 2
  • 16
  • 29
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
4

This will show you clues about why the linker doesn't want the installed library:

LD_DEBUG=all make ...

I had the same problem in a different context: my system /lib/libz.so.1 had unsatisfied dependencies on libc because I was trying to relink on a different version of the OS.

qneill
  • 1,643
  • 14
  • 18
2

for opensuse 12.3 (Dartmouth) (i586) sudo zypper install zlib-devel zlib-devel-static

euthygenes
  • 89
  • 1
  • 3
1

Others have mentioned that lib32z-dev solves the problem, but in general the required packages can be found here:

http://source.android.com/source/initializing.html See "Installing required packages"

Conley Owens
  • 8,691
  • 5
  • 30
  • 43