title: Rationale and intended use behind 'library-path', 'rpath', and 'rpath-link'
body: <p>I noticed many people, including me, do not accurately know the rationale and intended use behind the linker's search paths: 'library-path', 'rpath', and 'rpath-link'. Could someody explain this to us?</p>
  
  <h3>Context</h3>
  
  <p>After all my reading I am inclined to think 'library-path' and 'rpath' should be enough. The rationale and intended use of 'rpath-link' is a mystery to me. Let me explain why.<br>
  Setting the 'library-path' passes the specified paths to the linker to look for libraries at the specified paths at <em>link(/build) time</em>.<br>
  Setting the 'rpath' stores the specified paths in the built executable, the linker subsequently looks at the specified paths when the application is ran, i.e. at <em>run time</em>.<br>
  So, if your libraries reside at non-standard places, then you you need to specify 'library-path' to build your executable and 'rpath' to run it.<br>
  Now the GNU linker documentation says: "The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time."<br>
  This raises two questions:<br>
  1) If the 'r' in 'rpath-link' stands for 'runtime', then the name 'rpath-link' seems misleading, because it is used at <em>link time</em>.<br>
  2) I already specified my library directories for <em>link time</em> in the 'library-path'. I see no use for 'rpath-link'. Strange reality: from <a href="http://www.kaizou.org/2015/01/linux-libraries/" rel="nofollow noreferrer">http://www.kaizou.org/2015/01/linux-libraries/</a> I know 'library-path' is not used for secondary dependencies (a dependency from one shared library to another shared library). To resolve these secondary dependencies you need to set 'rpath' or 'rpath-link'. My question then is: what is the reason you may not use 'library-path' to <em>also</em> specify secondary dependencies and instead are obliged to use 'rpath-link'?</p>
  
  <h3>How I stumbled upon this problem</h3>
  
  <p>I have an executable called app which wants to use functionality from liba.so, so I linked app to liba.so. liba.so depends on libb.so. Both liba.so and libb.so reside in directory mylibs. When I wanted to build my executable I ran into the message:</p>
  
  <pre><code>/usr/bin/ld: warning: libb.so.80, needed by /.../liba.so, not found (try using -rpath or -rpath-link)
  </code></pre>
  
  <p>Followed by several undefined reference errors.<br>
  To properly build it I have to set both 'library-path' and 'rpath-link' (or 'rpath') to the directory mylibs. It puzzles me why 'library-path' is not also used to search for secondary dependency libb.so and that an additional 'rpath-link' must be set to search for this secondary dependency.</p>
  
  <p>Related question:
  <a href="https://stackoverflow.com/questions/38528878/rationale-of-rpath-link">Rationale of -rpath-link</a></p>