title: gcc link the shared library in specified path but not in the standard path
body: <p>I came across a strange issue when using gcc to link the shared library in specified path but not in the standard path.</p>
  
  <p>When I downloaded <a href="http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html" rel="nofollow">GNU readline library version 6.3</a> and compiled it successfully in path <strong>$HOME/Downloads</strong>.</p>
  
  <p>GNU readline library needs to link <strong>libtinfo</strong>, so I installed it by <code>sudo apt-get install libtinfo</code>.</p>
  
  <p>After that, I created a small sample test named <strong>rl.c</strong> to check it. To build my sample project, I also created two symbolic links like follow:</p>
  
  <pre><code>$ ls -lrt ~/Downloads/
  drwxr-xr-x 6 sfzhang sfzhang    4096 Mar  2 15:45 readline-6.3
  lrwxrwxrwx 1 sfzhang sfzhang      12 Mar  2 16:00 readline -&gt; readline-6.3
  
  $ ls -lrt ~/Downloads/readline-6.3/shlib/
  -rwxr-xr-x 1 sfzhang sfzhang 833856 Mar  2 15:36 libreadline.so.6.3
  lrwxrwxrwx 1 sfzhang sfzhang     18 Mar  2 16:28 libreadline.so -&gt; libreadline.so.6.3
  </code></pre>
  
  <p>To use the new built readline library, I export the path to <strong>LD_LIBRARY_PATH</strong>:</p>
  
  <pre><code>$ echo $LD_LIBRARY_PATH
  /home/sfzhang/Downloads/readline/shlib
  </code></pre>
  
  <p>And, I compiled <strong>rl.c</strong> using the following command:</p>
  
  <pre><code>$ gcc -o rl rl.c -I$HOME/Downloads -L$HOME/Downloads/readline/shlib -lreadline -ltinfo
  </code></pre>
  
  <p>Checking the <strong>rl</strong> linked libraries:</p>
  
  <pre><code>$ ldd rl
  linux-vdso.so.1 =&gt;  (0x00007fffe09a3000)
  libreadline.so.6 =&gt; /lib/x86_64-linux-gnu/libreadline.so.6 (0x00007fe22d243000)
  libtinfo.so.5 =&gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fe22d01a000)
  libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe22cc8d000)
  /lib64/ld-linux-x86-64.so.2 (0x00007fe22d492000)
  </code></pre>
  
  <p>I also tried anther command, but got the same result:</p>
  
  <pre><code>gcc -o rl rl.c -I$HOME/Downloads -L$HOME/Downloads/readline/shlib -lreadline -ltinfo -Wl,-rpath,$HOME/Downloads/readline/shlib
  </code></pre>
  
  <p>So, why the <strong>rl</strong> was liked to <strong>/lib/x86_64-linux-gnu/libreadline.so.6</strong> but not <strong>$HOME/Downloads/readline/shlib/libreadline.so</strong>.</p>
  
  <p>OS: Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1+deb7u1 x86_64 GNU/Linux</p>
  
  <p>gcc: gcc version 4.7.2 (Debian 4.7.2-5)</p>
  
  <p>ldd: ldd (Debian EGLIBC 2.13-38+deb7u6) 2.13</p>