title: libpng16.so.16: undefined reference to `inflateReset2@ZLIB_1.2.3.4'
body: <p>I am building a fork of caffe (3D-caffe), on my university cluster which has a different builds of different versions of different software in <code>/software/software/&lt;package&gt;/&lt;version-compiler&gt;/</code></p>
  
  <p>When cmake executes this compilation step:</p>
  
  <pre><code>g++ -fPIC -Wall -Wno-sign-compare -Wno-uninitialized -O3 -DNDEBUG \
  CMakeFiles/caffe.bin.dir/caffe.cpp.o  -o caffe -rdynamic ../lib/libcaffe.so \
  ../lib/libproto.a /software/software/Boost/1.61.0-foss-2016a/lib/libboost_system.so \
  /software/software/Boost/1.61.0-foss-2016a/lib/libboost_thread.so \
  -lpthread -lglog /software/software/gflags/2.1.2-foss-2016a/lib/libgflags.so \
  -lprotobuf -lpthread -lglog \
  /software/software/gflags/2.1.2-foss-2016a/lib/libgflags.so \
  -lprotobuf -lhdf5_hl -lhdf5 -lrt -lsz -lz -ldl -lm -lpthread -llmdb -lleveldb -lsnappy \
  -lcudart -lcurand -lcublas -lcudnn \
  /software/software/OpenCV/3.1.0-foss-2016a/lib/libopencv_highgui.so.3.1.0 \
  /software/software/OpenCV/3.1.0-foss-2016a/lib/libopencv_imgcodecs.so.3.1.0 \
  /software/software/OpenCV/3.1.0-foss-2016a/lib/libopencv_imgproc.so.3.1.0 \
  /software/software/OpenCV/3.1.0-foss-2016a/lib/libopencv_core.so.3.1.0 \
  -lopenblas -Wl,-rpath,/home/p253591/3D-Caffe/build/lib:/software/software/Boost/1.61.0-foss-2016a/lib:/software/software/gflags/2.1.2-foss-2016a/lib
  </code></pre>
  
  <p>TL;DR: The essentials form is (note the <code>-lz</code> for linking to <code>libz</code>):</p>
  
  <pre><code>g++ ... .../caffe.cpp.o  -o caffe .../libcaffe.so .../libproto.a .../libboost_system.so ...more .so's and -lxyz's... -lz
  </code></pre>
  
  <p>It errors:</p>
  
  <pre><code>/software/software/libpng/1.6.23-foss-2016a/lib/libpng16.so.16: undefined reference to `inflateReset2@ZLIB_1.2.3.4'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>And this seems to be correct, because when I specify the <code>-print-file-name=libz.so.1</code> option to <code>g++</code>, it prints <code>/lib/../lib64/libz.so.1</code>, which is slightly too old regarding version.</p>
  
  <p>I have a newer version in <code>/software/software/zlib/1.2.8-foss-2016a/lib/</code>, but when I:</p>
  
  <ul>
  <li>prepend this path to <code>LD_LIBARY_PATH</code>,</li>
  <li>append it to the <code>-rpath</code> option, or</li>
  <li>add it using the <code>-L</code> option,</li>
  </ul>
  
  <p><code>g++</code> keeps using <code>/lib/../lib64/libz.so.1</code> as it reports using <code>-print-file-name</code></p>
  
  <p>What does work, is removing the <code>-lz</code> option and adding <code>/software/software/zlib/1.2.8-foss-2016a/lib/libz.so.1</code> explicitly as a file to include during compilation/linking. But that seems like a hack.</p>
  
  <p>How can I tell <code>g++</code> to use <code>/software/software/zlib/1.2.8-foss-2016a/lib/libz.so.1</code> instead of <code>/lib/../lib64/libz.so.1</code> when fulfilling the <code>-lz</code> option? Preferably indirectly via cmake (when building caffe).</p>
  
  <p>Thank you in advance!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><code>ldd</code> states the right <code>.so</code> is used:</p>
  
  <pre><code># ldd /software/software/libpng/1.6.23-foss-2016a/lib/libpng16.so.16
          linux-vdso.so.1 =&gt;  (0x00007fff55599000)
          libz.so.1 =&gt; /software/software/zlib/1.2.8/lib/libz.so.1 (0x00007f7875b29000)
          libm.so.6 =&gt; /lib64/libm.so.6 (0x00007f7875888000)
          libpthread.so.0 =&gt; /lib64/libpthread.so.0 (0x00007f787566b000)
          libc.so.6 =&gt; /lib64/libc.so.6 (0x00007f78752d7000)
          /lib64/ld-linux-x86-64.so.2 (0x0000003328800000)
  </code></pre>
  
  <p>Moreover, the symbol should be there in this libz.so.1:</p>
  
  <pre><code># nm /software/software/zlib/1.2.8/lib/libz.so.1
  ...
  0000000000008270 T inflateReset2
  ...
  0000000000000000 A ZLIB_1.2.3.4
  ....
  #
  </code></pre>
  
  <p>I'm assuming the <code>@</code>-notation in de <code>undefined symbol</code>-error somehow needs the A-type symbol to be there as a version check (but I could not find any documentation on this in <code>ld.so</code> man pages)</p>