title: Linking to a dynamic library on a Mac with full path
body: <p>I am linking a (Python extension) library that embeds the Matlab engine with the following command (generated using cmake)</p>
  
  <pre><code>c++ -mmacosx-version-min=10.6 -bundle -headerpad_max_install_names  -o library.so library.o /Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib /Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib -framework Python
  </code></pre>
  
  <p>resulting in </p>
  
  <pre><code>$ otool -L library.so
  library.so:
      @loader_path/libeng.dylib (compatibility version 0.0.0, current version 0.0.0)
      @loader_path/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
      /System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.1)
      /opt/local/lib/gcc44/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.13.0)
      /opt/local/lib/gcc44/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
      /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0)
  </code></pre>
  
  <p>However, when I try to use the library, I get an error message:</p>
  
  <pre><code>ImportError: dlopen(./library.so, 2): Library not loaded: @loader_path/libmex.dylib
    Referenced from: ./library.so
    Reason: image not found
  </code></pre>
  
  <p>I believe the problem stems from the fact that the linker includes the matlab dylib files in the form <code>@loader_path/libeng.dylib</code> rather than using the full path, even though I give the full path to <code>g++</code>. How can I force the linker to use the full path?</p>
  
  <p>I know one solution is to use </p>
  
  <pre><code>export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2009b.app/bin/maci64:$DYLD_LIBRARY_PATH
  </code></pre>
  
  <p>which is where those library files reside, but I'd like to avoid that as it causes some other problems. </p>