title: Why I cannot override search path of dynamic libraries with LD_LIBRARY_PATH?
body: <p>Edit: I resolved this issue, the solution is below.</p>
  
  <p>I am building a code in a shared computing cluster dedicated for scientific computing, thus I can only control files in my home folder. Although I am using fftw as an example, I would like to understand the specific reason, why my attempt to setup LD_LIBRARY_PATH does not work.</p>
  
  <p>I build the fftw and fftw_mpi libraries in my home folder like this</p>
  
  <pre><code>./configure --prefix=$HOME/install/fftw --enable-mpi --enable-shared
  make install
  </code></pre>
  
  <p>It builds fine, but in install/fftw/lib, I find that the freshly built libfftw3_mpi.so links to wrong version of fftw library.</p>
  
  <pre><code>$ ldd libfftw3_mpi.so |grep fftw
    libfftw3.so.3 =&gt; /usr/lib64/libfftw3.so.3 (0x00007f7df0979000)
  </code></pre>
  
  <p>If I now try to set the LD_LIBRARY_PATH correctly pointing to this directory, it still prefers the wrong library:</p>
  
  <pre><code>$ export LD_LIBRARY_PATH=$HOME/install/fftw/lib
  $ ldd libfftw3_mpi.so |grep fftw
      libfftw3.so.3 =&gt; /usr/lib64/libfftw3.so.3 (0x00007f32b4794000)
  </code></pre>
  
  <p>Only if I explicitly use LD_PRELOAD, I can override this behavior. I don't think LD_PRELOAD is a proper solution though.</p>
  
  <pre><code>$ export LD_PRELOAD=$HOME/install/fftw/lib/libfftw3.so.3 
  $ ldd libfftw3_mpi.so |grep fftw
     $HOME/install/fftw/lib/libfftw3.so.3 (0x00007f5ca3d14000)
  </code></pre>
  
  <p>Here is what I would have expecting, a small test done in Ubuntu desktop, where I installed fftw to /usr/lib first, and then override this search path with LD_LIBRARY_PATH.</p>
  
  <pre><code>$ export LD_LIBRARY_PATH=
  $ ldd q0test_mpi |grep fftw3
      libfftw3.so.3 =&gt; /usr/lib/x86_64-linux-gnu/libfftw3.so.3
  $ export LD_LIBRARY_PATH=$HOME/install/fftw-3.3.4/lib
  $ ldd q0test_mpi |grep fftw3
      libfftw3.so.3 =&gt; $HOME/install/fftw-3.3.4/lib/libfftw3.so.3
  </code></pre>
  
  <p>In short: Why is libfft3_mpi library still finding the wrong dynamic fftw3 library? Where is this searchpath hard coded in a such way that it is prioritized over LD_LIBARY_PATH? Why is this is not the case in another computer?</p>
  
  <p>I am using intel compilers 13.1.2, mkl 11.0.4.183 and openmpi 1.6.2 if this matters.</p>
  
  <p>Edit: Thanks for all the answers. With help of those, we were able to isolate the problem to RPATH, and from there, the cluster support was able to figure out the problem. I accepted the first answer, but both answers were good.</p>
  
  <p>The reason, why this was so hard to figure out, is that we did not know that the compilers were actually wrapper scripts, adding things to compiler command line. Here a part of a reply from the support:</p>
  
  <blockquote>
    <p>[The] compilation goes through our compiler wrapper. We do RPATH-ing
    by default as it helps most users in correctly running their jobs
    without loading LD-LIBRARY_PATH etc. However we exclude certain
    library paths from default RPATH which includes /lib, /lib64 /proj
    /home etc. Earlier the /usr/lib64 was not excluded by mistake
    (mostly). Now we have added that path in the exclusion list.</p>
  </blockquote>