Undefined reference to inflateEnd (zlib linking problem)

My project uses zlib:

println!("cargo:rustc-link-lib=static=zlib");

which was working but now gives:

error: could not find native static library zlib, perhaps an -L flag is missing?

    ldconfig -p | grep libz
    libz3.so.4 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz3.so.4
    libz3.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libz3.so
    libzstd.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libzstd.so.1
    libz.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so.1
    libz.so.1 (libc6) => /lib32/libz.so.1
    libz.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so

If I change to dynamic:

println!("cargo:rustc-link-lib=zlib");

I get lots of undefined reference errors related to zlib:

zmbv.c:(.text.unlikely+0x16): undefined reference to inflateEnd'`

also tried

println!("cargo:rustc-link-lib=dylib=z");
println!("cargo:rustc-link-lib=z");
println!("cargo:rustc-link-lib=static=z");

adding

println!("cargo:rustc-link-search=/usr/lib/x86_64-linux-gnu/");
println!("cargo:rustc-link-lib=static=z");

makes the 'zlib not found' (on static mode) error go away but I still get the same undefined references

The error is coming from within zmbv.c which is part of libavcodec. Whichever crate is linking libavcodec should also be responsible for linking all of its dependencies.

Your previous topic mentioned using the ffmeg-sys-next crate. This crate will automatically find and link all the necessary libraries by default, using pkg-config. You shouldn't need your own build script.

For example, just by adding this dependency and calling an avformat function, my test program gets linked to all of these libraries, without a build script:

$ cat Cargo.toml 
[package]
name = "ff"
version = "0.1.0"
edition = "2018"

[dependencies]
ffmpeg-sys-next = "4"

$ cat src/main.rs 
fn main() {
    unsafe { ffmpeg_sys_next::avformat_flush(0 as _); }
}

$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s

$ ldd ./target/debug/ff
	linux-vdso.so.1 (0x00007ffd183ff000)
	libavformat.so.58 => /usr/lib/x86_64-linux-gnu/libavformat.so.58 (0x00007f58558f1000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f58558eb000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f58558c8000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f58558ad000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f58556bb000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f5855bbc000)
	libavcodec.so.58 => /usr/lib/x86_64-linux-gnu/libavcodec.so.58 (0x00007f58540fe000)
	libavutil.so.56 => /usr/lib/x86_64-linux-gnu/libavutil.so.56 (0x00007f5853fd3000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5853e84000)
	libxml2.so.2 => /usr/lib/x86_64-linux-gnu/libxml2.so.2 (0x00007f5853cca000)
	libbz2.so.1.0 => /lib/x86_64-linux-gnu/libbz2.so.1.0 (0x00007f5853cb7000)
	libgme.so.0 => /usr/lib/x86_64-linux-gnu/libgme.so.0 (0x00007f5853c6a000)
	libopenmpt.so.0 => /usr/lib/x86_64-linux-gnu/libopenmpt.so.0 (0x00007f5853a7d000)
	libchromaprint.so.1 => /usr/lib/x86_64-linux-gnu/libchromaprint.so.1 (0x00007f5853a65000)
	libbluray.so.2 => /usr/lib/x86_64-linux-gnu/libbluray.so.2 (0x00007f5853a14000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f58539f8000)
	libgnutls.so.30 => /usr/lib/x86_64-linux-gnu/libgnutls.so.30 (0x00007f5853822000)
	libssh-gcrypt.so.4 => /usr/lib/x86_64-linux-gnu/libssh-gcrypt.so.4 (0x00007f5853796000)
	libswresample.so.3 => /usr/lib/x86_64-linux-gnu/libswresample.so.3 (0x00007f5853772000)
	libvpx.so.6 => /usr/lib/x86_64-linux-gnu/libvpx.so.6 (0x00007f585353a000)
	libwebpmux.so.3 => /usr/lib/x86_64-linux-gnu/libwebpmux.so.3 (0x00007f5853330000)
	libwebp.so.6 => /usr/lib/x86_64-linux-gnu/libwebp.so.6 (0x00007f58530c7000)
	liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f585309e000)
	librsvg-2.so.2 => /usr/lib/x86_64-linux-gnu/librsvg-2.so.2 (0x00007f58527ae000)
	libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f585274e000)
	libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f5852623000)
	libcairo.so.2 => /usr/lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f5852500000)
	libzvbi.so.0 => /usr/lib/x86_64-linux-gnu/libzvbi.so.0 (0x00007f5852471000)
	libsnappy.so.1 => /usr/lib/x86_64-linux-gnu/libsnappy.so.1 (0x00007f5852466000)
	libaom.so.0 => /usr/lib/x86_64-linux-gnu/libaom.so.0 (0x00007f5851fc8000)
	libcodec2.so.0.9 => /usr/lib/x86_64-linux-gnu/libcodec2.so.0.9 (0x00007f58511e4000)
	libgsm.so.1 => /usr/lib/x86_64-linux-gnu/libgsm.so.1 (0x00007f58511d3000)
	libmp3lame.so.0 => /usr/lib/x86_64-linux-gnu/libmp3lame.so.0 (0x00007f585115b000)
	libopenjp2.so.7 => /usr/lib/x86_64-linux-gnu/libopenjp2.so.7 (0x00007f5851105000)
	libopus.so.0 => /usr/lib/x86_64-linux-gnu/libopus.so.0 (0x00007f58510a6000)
	libshine.so.3 => /usr/lib/x86_64-linux-gnu/libshine.so.3 (0x00007f5850e9b000)
	libspeex.so.1 => /usr/lib/x86_64-linux-gnu/libspeex.so.1 (0x00007f5850e7d000)
	libtheoraenc.so.1 => /usr/lib/x86_64-linux-gnu/libtheoraenc.so.1 (0x00007f5850e3e000)
	libtheoradec.so.1 => /usr/lib/x86_64-linux-gnu/libtheoradec.so.1 (0x00007f5850e1e000)
	libtwolame.so.0 => /usr/lib/x86_64-linux-gnu/libtwolame.so.0 (0x00007f5850df8000)
	libvorbis.so.0 => /usr/lib/x86_64-linux-gnu/libvorbis.so.0 (0x00007f5850dca000)
	libvorbisenc.so.2 => /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2 (0x00007f5850d1f000)
	libwavpack.so.1 => /usr/lib/x86_64-linux-gnu/libwavpack.so.1 (0x00007f5850cf3000)
	libx264.so.155 => /usr/lib/x86_64-linux-gnu/libx264.so.155 (0x00007f5850a33000)
	libx265.so.179 => /usr/lib/x86_64-linux-gnu/libx265.so.179 (0x00007f584fac5000)
	libxvidcore.so.4 => /usr/lib/x86_64-linux-gnu/libxvidcore.so.4 (0x00007f584f9b2000)
	libva.so.2 => /usr/lib/x86_64-linux-gnu/libva.so.2 (0x00007f584f989000)
	libva-drm.so.2 => /usr/lib/x86_64-linux-gnu/libva-drm.so.2 (0x00007f584f984000)
	libva-x11.so.2 => /usr/lib/x86_64-linux-gnu/libva-x11.so.2 (0x00007f584f97a000)
	libvdpau.so.1 => /usr/lib/x86_64-linux-gnu/libvdpau.so.1 (0x00007f584f974000)
	libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f584f837000)
	libdrm.so.2 => /usr/lib/x86_64-linux-gnu/libdrm.so.2 (0x00007f584f823000)
	libOpenCL.so.1 => /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 (0x00007f584f618000)
	libicuuc.so.66 => /usr/lib/x86_64-linux-gnu/libicuuc.so.66 (0x00007f584f432000)
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f584f24f000)
	libmpg123.so.0 => /usr/lib/x86_64-linux-gnu/libmpg123.so.0 (0x00007f584f1ef000)
	libvorbisfile.so.3 => /usr/lib/x86_64-linux-gnu/libvorbisfile.so.3 (0x00007f584f1e4000)
	libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f584f19d000)
	libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f584f0de000)
	libp11-kit.so.0 => /usr/lib/x86_64-linux-gnu/libp11-kit.so.0 (0x00007f584efa6000)
	libidn2.so.0 => /usr/lib/x86_64-linux-gnu/libidn2.so.0 (0x00007f584ef85000)
	libunistring.so.2 => /usr/lib/x86_64-linux-gnu/libunistring.so.2 (0x00007f584ee03000)
	libtasn1.so.6 => /usr/lib/x86_64-linux-gnu/libtasn1.so.6 (0x00007f584eded000)
	libnettle.so.7 => /usr/lib/x86_64-linux-gnu/libnettle.so.7 (0x00007f584edb3000)
	libhogweed.so.5 => /usr/lib/x86_64-linux-gnu/libhogweed.so.5 (0x00007f584ed7b000)
	libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f584ecf5000)
	libgcrypt.so.20 => /usr/lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f584ebd7000)
	libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f584ebb4000)
	libgssapi_krb5.so.2 => /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007f584eb67000)
	libsoxr.so.0 => /usr/lib/x86_64-linux-gnu/libsoxr.so.0 (0x00007f584eafc000)
	libcairo-gobject.so.2 => /usr/lib/x86_64-linux-gnu/libcairo-gobject.so.2 (0x00007f584eaee000)
	libgdk_pixbuf-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f584eac6000)
	libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f584e8e5000)
	libpangocairo-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f584e8d3000)
	libpango-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f584e884000)
	libffi.so.7 => /usr/lib/x86_64-linux-gnu/libffi.so.7 (0x00007f584e878000)
	libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f584e803000)
	libpixman-1.so.0 => /usr/lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f584e75c000)
	libpng16.so.16 => /usr/lib/x86_64-linux-gnu/libpng16.so.16 (0x00007f584e724000)
	libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f584e71f000)
	libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f584e6f5000)
	libxcb-render.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f584e6e6000)
	libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f584e4da000)
	libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f584e4c5000)
	libogg.so.0 => /usr/lib/x86_64-linux-gnu/libogg.so.0 (0x00007f584e4b8000)
	libnuma.so.1 => /usr/lib/x86_64-linux-gnu/libnuma.so.1 (0x00007f584e4ab000)
	libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f584e4a1000)
	libicudata.so.66 => /usr/lib/x86_64-linux-gnu/libicudata.so.66 (0x00007f584c9e0000)
	libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f584c9b2000)
	libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f584c9a9000)
	libkrb5.so.3 => /usr/lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007f584c8cc000)
	libk5crypto.so.3 => /usr/lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007f584c899000)
	libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007f584c892000)
	libkrb5support.so.0 => /usr/lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007f584c883000)
	libgomp.so.1 => /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f584c841000)
	libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f584c83b000)
	libmount.so.1 => /lib/x86_64-linux-gnu/libmount.so.1 (0x00007f584c7db000)
	libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f584c7ae000)
	libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f584c792000)
	libpangoft2-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f584c779000)
	libfribidi.so.0 => /usr/lib/x86_64-linux-gnu/libfribidi.so.0 (0x00007f584c75c000)
	libthai.so.0 => /usr/lib/x86_64-linux-gnu/libthai.so.0 (0x00007f584c751000)
	libharfbuzz.so.0 => /usr/lib/x86_64-linux-gnu/libharfbuzz.so.0 (0x00007f584c64c000)
	libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f584c644000)
	libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f584c63c000)
	libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007f584c635000)
	libblkid.so.1 => /lib/x86_64-linux-gnu/libblkid.so.1 (0x00007f584c5de000)
	libpcre2-8.so.0 => /usr/lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f584c54e000)
	libdatrie.so.1 => /usr/lib/x86_64-linux-gnu/libdatrie.so.1 (0x00007f584c542000)
	libgraphite2.so.3 => /usr/lib/x86_64-linux-gnu/libgraphite2.so.3 (0x00007f584c515000)
	libbsd.so.0 => /usr/lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f584c4fb000)

If you need to write your own -sys crate instead of using the existing one, you can use the pkg-config crate in your build script to help find and link the correct libraries.

thanks!!!!!!!!! I added the libraries in ffmpeg-sys-next and then my project which depends on ffmpeg-sys-next built correctly.

Why, though, I need for ffmpeg-sys-next to link with its dependencies? Why I cannot fill them later in my project? I understand that this is horrible, I'll definetely fill then in ffmpeg-sys-next already, but I'm curious why.

Also, your help made me fix the other problem of mine that you linked. It was the same problem: linking things in my lib, no ffmpeg-sys-next. If I remember correctly, on C++ I could do this, or I'm mistaken.

Anyways, you didn't get any errors because my ffmpeg-sys-next is not the same as the public one, I added new things, that's why it didn't link things correctly. I'm gonna make a pull request once things are working correctly.

the strange thing is that ffmpeg-sys-next built correctly without any of the dependencies. It's only when I link with my project that I get the error.

Don't use println!("cargo:rustc-link-lib in your projects, unless you're writing a separate sys crate.

Instead, add libz-sys to your project as a dependency. It does extra work to ensure that zlib can be found and linked properly. Additionally, using a shared sys crate ensures the same library won't be loaded twice. It would be problematic if two different versions of zlib got pulled in by different crates, so it's helpful if everyone uses the same sys crate instead of their own linker flags.

The order in which things are linked actually matters. The rust toolchain tries to take care of it for you, but to do that, it needs to know the true dependencies.