title: ld: Using -rpath,$ORIGIN inside a shared library (recursive)
body: <p>I just made a basic example of using ld's <code>-rpath</code> option with <code>$ORIGIN</code> <a href="https://stackoverflow.com/questions/6311016/building-a-simple-hello-world-esque-example-of-using-lds-option-rpath-with-o">here</a> (see 2nd response for a working version). I'm trying to create an example where <code>main.run</code> links to <code>foo.so</code>, which in turn links to <code>bar.so</code>, all using <code>rpath</code> and <code>$ORIGIN</code>.</p>
  <p>The run-time file-structure is:</p>
  <blockquote>
  <ul>
  <li>project/
  <ul>
  <li>lib/
  <ul>
  <li>dir/
  <ul>
  <li>sub/
  <ul>
  <li><strong>bar.so</strong></li>
  </ul>
  </li>
  <li><strong>foo.so</strong></li>
  </ul>
  </li>
  </ul>
  </li>
  <li>run/
  <ul>
  <li><strong>main.run</strong> (failing to build)</li>
  </ul>
  </li>
  </ul>
  </li>
  </ul>
  </blockquote>
  <p>I'm building foo.so using:</p>
  <pre><code>g++ -c -o obj/foo.o src/foo.cpp -fPIC
  g++ -shared -o lib/dir/foo.so obj/foo.o -Wl,-soname,foo.so -Wl,-rpath,'$ORIGIN/sub' -Llib/dir/sub -l:bar.so
  </code></pre>
  <p>Which builds fine. <code>ldd lib/dir/foo.so</code> can even find <code>bar.so</code>.</p>
  <p>However, when I try to link <code>main.run</code> to <code>foo.so</code>, <code>foo.so</code> can't find bar.so.</p>
  <p>I'm building main.so using:</p>
  <pre><code>g++ -c -o obj/main.o src/main.cpp
  g++ -o run/main.run obj/main.o -Wl,-rpath,'$ORIGIN/../lib/dir' -Llib/dir -l:foo.so
  </code></pre>
  <p>This works fine if another version of <code>foo.so</code> is used that doesn't recursively link. (Uncomment lines in make.sh, in project below, to test).</p>
  <p>However, using the normal <code>foo.so</code> I'm getting this error when building <code>main.run</code>:</p>
  <blockquote>
  <p>/usr/bin/ld: warning: bar.so, needed by lib/dir/foo.so, not found (try using -rpath or -rpath-link)</p>
  </blockquote>
  <p>So my questions are:</p>
  <ol>
  <li>Does <code>$ORIGIN</code> within foo.so resolve to <code>project/lib/dir</code> (where <code>foo.so</code> is) or <code>project/run</code> (where <code>main.run</code> (the executable linking it) is)?<br />
  ldd would seem to indicate that it's <code>project/lib/dir</code>, which would seem to be the best way (although I tried assuming both).</li>
  <li>How do I get these to link (while preserving relocatability) - preferably without using <code>-rpath-link</code>.</li>
  </ol>
  <p>You can download the project <a href="http://dl.dropbox.com/u/31990648/SourceForge/rpath_origin_recursive/project.zip" rel="noreferrer">here</a>. It's as simple as I can make it. 4 short sources and a script.<br />
  After extracting, just run <code>./make.sh</code> from within <code>project/</code>.</p>
  <p><strong>Note:</strong> I'm using <code>-l:</code>. This shouldn't change anything except that the libraries are named like <code>foo.so</code> instead of <code>libfoo.so</code>, and lunk with <code>-l:foo.so</code> instead of <code>-lfoo</code>.</p>