Where do I set DYLD_LIBRARY_PATH on Mac OS X, and is it a good idea?

I am trying to install a solver written in C++ on my Mac (OS X), for use with code I have written in XCode.

The solver documentation says this:

Be sure to have "." in your DYLD_LIBRARY_PATH in order to

  • run the ready-built executables
  • link with the libamg.dylib (and the gfortran RTSlibs)

I don't really understand what this means. Where and what do I need to change what?

I have done some googling, but haven't come across anything that is simple enough for a newbie like me! If there are any patient people out there who wouldn't mind directing me to an online resource or giving me the a-b-cs of how and where to set environment variables, I would be very grateful.


It's an environment variable and as such is usually set in Terminal by

export DYLD_LIBRARY_PATH=someValue

man dyld says:

DYLD_LIBRARY_PATH

This is a colon separated list of directories that contain libraries. The dynamic linker searches these directories before it searches the default locations for libraries. It allows you to test new versions of existing libraries.

For each library that a program uses, the dynamic linker looks for it in each directory in DYLD_LIBRARY_PATH in turn. If it still can't find the library, it then searches DYLD_FALLBACK_FRAMEWORK_PATH and DYLD_FALLBACK_LIBRARY_PATH in turn.

Use the -L option to otool(1). to discover the frameworks and shared libraries that the executable is linked against.


You'd probably want something like

export DYLD_LIBRARY_PATH=.:$DYLD_LIBRARY_PATH

to prepend . (current directory) to the list of locations searched. On my unmodified OS X, DYLD_LIBRARY_PATH has no current value though:

$ echo $DYLD_LIBRARY_PATH

$

Depending on how you intent to run your program, you'd need to set this differently, e.g. in Xcode (I don't know where though).


One should never set export DYLD_LIBRARY_PATH on your system.

Shared library paths can be fixed using otool -L and install_name_tool.

For instance, if you compile Perl DBD-MySQL you won't be able to use it as the linker doesn't know where you've installed MySQL.

># make
....


># otool -L blib/arch/auto/DBD/mysql/mysql.bundle

blib/arch/auto/DBD/mysql/mysql.bundle:
        libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)


#> install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib blib/arch/auto/DBD/mysql/mysql.bundle


># otool -L blib/arch/auto/DBD/mysql/mysql.bundle

blib/arch/auto/DBD/mysql/mysql.bundle:
        /usr/local/mysql/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)



#> make test
...
Result: PASS

#> make install

This is as simple as that.


In Xcode 4 you can add it to the project Scheme to avoid errors like this one:

dyld: Library not loaded: @loader_path/libLeap.dylib
  Referenced from: /Users/paulsolt/Library/Developer/Xcode/DerivedData/LeapTest-eqcxmzewheyjusgrcszyvlcxlgna/Build/Products/Debug/LeapTest
  Reason: image not found
  1. In the Menu click on "Product" -> "Edit Scheme" -> "Arguments" tab -> Add "Environment Variables" -> Key: DYLD_LIBRARY_PATH Value: /Users/MyUserAccount/path/to/lib

  2. Change the path to your user account and the full path to the library folder.

  3. You should be able to build and run.

Setting DYLD_LIBRARY_PATH in Xcode 4