How to fix C++ error "undefined reference to `zlibVersion'"
Problem:
When compiling your C++ application, you see one or more of the following error message (complete error message log example shown below)
version.c:(.text+0x5d): undefined reference to `zlibVersion'
content_encoding.c:(.text+0x1f8): undefined reference to `inflateInit_'
content_encoding.c:(.text+0x2b5): undefined reference to `inflateEnd'
/usr/bin/ld: content_encoding.c:(.text+0x401): undefined reference to `inflateInit2_'
Solution
You need to link zlib
to your executable - the library that needs to be linked is called z
(libz.so
). When running gcc
manually, use the -lz
option, for example:
gcc -o myexe main.cpp -lz
When using CMake, use
add_executable(myexe main.cpp)
target_link_libraries(myexe z)
Full error log example:
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-version.o): in function `curl_version':
version.c:(.text+0x5d): undefined reference to `zlibVersion'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-version.o): in function `curl_version_info':
version.c:(.text+0x169): undefined reference to `zlibVersion'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `deflate_init_writer':
content_encoding.c:(.text+0x1f8): undefined reference to `inflateInit_'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `gzip_close_writer':
content_encoding.c:(.text+0x2b5): undefined reference to `inflateEnd'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `deflate_close_writer':
content_encoding.c:(.text+0x345): undefined reference to `inflateEnd'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `gzip_init_writer':
content_encoding.c:(.text+0x3ce): undefined reference to `zlibVersion'
/usr/bin/ld: content_encoding.c:(.text+0x401): undefined reference to `inflateInit2_'
/usr/bin/ld: content_encoding.c:(.text+0x429): undefined reference to `inflateInit2_'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `inflate_stream':
content_encoding.c:(.text+0x50e): undefined reference to `inflate'
/usr/bin/ld: content_encoding.c:(.text+0x691): undefined reference to `inflateEnd'
/usr/bin/ld: content_encoding.c:(.text+0x6f4): undefined reference to `inflateEnd'
/usr/bin/ld: content_encoding.c:(.text+0x724): undefined reference to `inflateEnd'
/usr/bin/ld: content_encoding.c:(.text+0x73d): undefined reference to `inflateInit2_'
/usr/bin/ld: content_encoding.c:(.text+0x7d9): undefined reference to `inflateEnd'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `deflate_unencode_write':
content_encoding.c:(.text+0x8c0): undefined reference to `inflateEnd'
/usr/bin/ld: content_encoding.c:(.text+0x8f1): undefined reference to `inflateEnd'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o): in function `gzip_unencode_write':
content_encoding.c:(.text+0xa40): undefined reference to `inflateEnd'
/usr/bin/ld: content_encoding.c:(.text+0xb07): undefined reference to `inflateEnd'
/usr/bin/ld: /home/uli/.conan/data/libcurl/7.69.1/_/_/package/d66c87f35a2a176e10af56a7f32a51d6e7b43e26/lib/libcurl.a(libcurl_la-content_encoding.o):content_encoding.c:(.text+0xc02): more undefined references to `inflateEnd' follow