title: How to set the path that a .so library will search for other .so libraries?
body: <p>I have a libA.so that depends on libB.so, which is located at ../libB/ (from libA.c). I'm trying to compile things in such a way that I don't have to set any environment variables.  I have:</p>
  
  <pre><code>    cc -std=c99 -c -fPIC -I../libB/ -Wall libA.c
      cc -std=c99 -shared libA.o -L../libB -lB -o libA.so
  </code></pre>
  
  <p>This compiles fine.  When I run a program that loads libA with dlopen I get:</p>
  
  <pre><code>dyld: Library not loaded: libB.so
    Referenced from: libA/libA.so
    Reason: image not found
  Trace/BPT trap: 5
  </code></pre>
  
  <p>so libA is not finding libB at runtime.  I found this solution to change the runtime path on Mac OS X:</p>
  
  <p>install_name_tool -change libB.so @loader_path/../libB.so libA.so</p>
  
  <p>but I'd like to find a solution that would work on both OS X and Linux.  Again, I'm trying to make the end-user do as little as possible so I don't want them to have to set environment variables, and I have to use cc (which for me is Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn), and I'd like for it to work on Linux too, so presumably cc=gcc there).</p>
  
  <p><strong>EDIT</strong>  My problem may be more complicated than I realized.  I'm making this dynamic library in C, but trying to use it from within python.  I can use libB.so (which has no dependencies) from within python no problem, and when I load libA.so from within python it finds it (see error above), it's just that at that point libA.so realizes it doesn't know where to find libB.so.  If I understand your answers correctly below, the solutions depend on setting the linker path when you compile the executable, which in my case is in python.  </p>
  
  <p><strong>Is there no way to tell libA.so where to look for libB.so when I compile it?  I can do it afterward with install_name_tool on OSX, but is there not a way with the compiler that would work on both OSX and linux?</strong></p>