title: What is the difference between LD_LIBRARY_PATH and -L at link time?
body: <p>I'm having problems with <code>LD_LIBRARY_PATH</code> at <strong>link</strong> time (this question has nothing to do with run time).</p>
  
  <p>The link line looks like this when I run make (this is a Linux system using g++ version 4.1.x):</p>
  
  <pre><code>g++ a.o b.o c.o -o myapp \
   -L/long/path/to/libs/ \
   -L/another/long/path/ \
   -labc -ldef -lghi
  </code></pre>
  
  <p>The <code>-l</code> options reference shared libraries (e.g., libabc.so) which exist in the directories specified by the <code>-L</code> options.  Those directories also appear in <code>LD_LIBRARY_PATH</code>.  With that configuration, the link is successful, and I can run the application.</p>
  
  <p>If I remove the directories from <code>LD_LIBRARY_PATH</code>, then I get a single error line such as:</p>
  
  <pre><code>/usr/bin/ld: cannot find -labc
  </code></pre>
  
  <p>On the other hand, if I remove the directories from the list of <code>-L</code> options, then I get many warnings such as:</p>
  
  <pre><code>/usr/bin/ld: warning: libabc.so, needed by /long/path/to/libs/libxyz.so,
      not found (try using -rpath or -rpath-link)
  </code></pre>
  
  <p>and then many more errors, such as:</p>
  
  <pre><code>/long/path/to/libs/libdef.so: undefined reference to `Foo::Bar&lt;Baz&gt;::junk(Fred*)'
  </code></pre>
  
  <p>Can someone explain the difference between <code>LD_LIBRARY_PATH</code> and <code>-L</code>?  I would like to understand this stuff in depth, so references are greatly appreciated!</p>
  
  <p>Also, what do I have to add to the link line to avoid using <code>LD_LIBRARY_PATH</code>?</p>
  
  <p><strong>EDIT:</strong> When directories were missing from <code>-L</code>, the compiler suggested to "try using -rpath or -rpath-link".  I don't think I've ever seen those options in a makefile before.  Have you?  Not sure if that would help the <code>LD_LIBRARY_PATH</code> problem though.</p>