Undefined reference to zlib in Ubuntu 11.10

Asked by Justin

For our project CMake ends up creating a command like:
/usr/bin/c++ -Wall -Wextra -Wno-missing-field-initializers -Werror -Wno-unused-parameter -pipe -fPIC -mtune=core2 -mfpmath=sse -g -O0 -lXxf86vm CMakeFiles/BulkDataEditor.dir/BulkDataEditor.cpp.o CMakeFiles/BulkDataEditor.dir/BulkDataEditorMain.cpp.o CMakeFiles/BulkDataEditor.dir/BulkDataEditorRegistration.cpp.o CMakeFiles/BulkDataEditor.dir/DiscreteValueManager.cpp.o CMakeFiles/BulkDataEditor.dir/moc_BulkDataEditor.cxx.o -o ../../../bin/BulkDataEditor -Wl,--start-group -rdynamic -L/mnt/data/Projects/WeatherscapeXT/trunk/Build/ThirdParty/lib -lz
<bunch of third party libraries>

This built fine in 11.04 but under 11.10 it fails with:
undefined reference to symbol 'inflateInit2_'
/usr/bin/ld: note: 'inflateInit2_' is defined in DSO /usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/libz.so so try adding it to the linker command line
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/libz.so: could not read symbols: Invalid operation

During cmake build step I can see zlib has been found successfully using cmake 2.8.5
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "")

Removing the -lz from the build line I get a bunch of errors showing that zlib isn't referenced (as I would expect). So what is special about inflateInit2_?

../../Components/Zip/liblibZip.a(ZipArchive.cpp.o): In function `WeatherscapeXT::ZipArchive::open()':
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:53: undefined reference to `unzOpen'
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:61: undefined reference to `zipOpen'
../../Components/Zip/liblibZip.a(ZipArchive.cpp.o): In function `WeatherscapeXT::ZipArchive::close()':
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:79: undefined reference to `unzCloseCurrentFile'
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:84: undefined reference to `zipClose'
../../Components/Zip/liblibZip.a(ZipArchive.cpp.o): In function `WeatherscapeXT::ZipArchive::listFiles()':
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:93: undefined reference to `unzGetGlobalInfo'
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:101: undefined reference to `unzGoToFirstFile'
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:108: undefined reference to `unzGetCurrentFileInfo'
/mnt/data/Projects/WeatherscapeXT/trunk/Source/Components/Zip/ZipArchive.cpp:125: undefined reference to `unzGoToNextFile'


Question information

English Edit question
Ubuntu zlib Edit question
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :

The combination of messages

"Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "")"
"/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/libz.so: could not read symbols"

can perhaps be caused by the file being there but being broken.

As a first idea try reinstalling zlib1g-dev.

Revision history for this message
Justin (justinr) said :

Yup I tried that but it didn't work. I'll attempt to compile one manually and see what I get

Revision history for this message
Simon (simonhyll) said :

If you download the sources of ZLib and do a simple "grep -irl unzGoToFirstFile" you'll find that no, unzGoToFirstFile is not part of ZLib. It is however part of a contrib library found inside the ZLib sources called minizip. Minizip requires separate compilation and you need to link towards that as well.

I solved this by adding the following to the ZLib CMakeListst.txt file:

set(UNZIP_SOURCES "contrib/minizip/unzip.c"
set(UNZIP_HEADERS "contrib/minizip/unzip.h"

add_library(unzip STATIC

target_link_libraries(unzip zlibstatic)

    install(TARGETS unzip


Then you have to link towards the resulting libunzip.a library.

You can easily modify the above code to link a shared library, just rename zlibstatic to just zlib, and replace STATIC with SHARED

Can you help with this problem?

Provide an answer of your own, or ask Justin for more information if necessary.

To post a message you must log in.