title: g++ compile error: undefined reference to a shared library function which exists
body: <p>I recently installed the <strong>hdf5</strong> library on an ubuntu machine, and am now having trouble linking to the exported functions. I wrote a simple test script <strong>readHDF.cpp</strong> to explain the issue:</p>
  
  <pre><code>#include &lt;hdf5.h&gt;
  
  int main(int argc, char * argv[])
  {
    hid_t     h5_file_id = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
    return 0;
  }
  </code></pre>
  
  <p>The compile command is</p>
  
  <pre><code>g++ -Wl,-rpath,$HOME/hdf5/lib -I$HOME/hdf5/include \
      -L$HOME/hdf5/lib -l:$HOME/hdf5/lib/libhdf5.so readHDF.cpp
  </code></pre>
  
  <p>which returns the following error</p>
  
  <pre><code>/tmp/cc6DXdxV.o: In function `main':  
  readHDF.cpp:(.text+0x1f): undefined reference to `H5check_version'  
  readHDF.cpp:(.text+0x3c): undefined reference to `H5Fopen'  
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I am confused because the <strong>nm</strong> command seems to say that the function has been exported:</p>
  
  <pre><code>nm -C $HOME/hdf5/lib/libhdf5.so | grep H5check_version
  </code></pre>
  
  <p>which returns</p>
  
  <pre><code>0000000000034349 T H5check_version
  </code></pre>
  
  <p>and a similar result for <code>H5Fopen</code>. Any thoughts on what might be going wrong? Not sure if it helps, but if I comment out the <code>H5Fopen</code> portion of the script, then it compiles fine:</p>
  
  <pre><code>#include &lt;hdf5.h&gt;
  
  int main(int argc, char * argv[])
  {
  hid_t     h5_file_id;// = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
  return 0;
  }
  </code></pre>
  
  <p>Also there are multiple versions of hdf5 installed on the server which are used by various python modules such as h5py and tables, but I couldn't get any of them to work, so I installed this version in my local directory and changed the rpath options for g++ linker.</p>