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

28

16

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.

Ant

Posted 2011-05-12T07:24:25.477

Reputation: 397

Answers

20

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).

Daniel Beck

Posted 2011-05-12T07:24:25.477

Reputation: 98 421

1

I agree with @TVNshack that you should usually not set DYLD_LIBRARY_PATH nor DYLD_FRAMEWORK_PATH, because it may prevent those loaded libs to find system-provided libs. To make that work better, use instead the FALLBACK versions. More info in this answer: http://stackoverflow.com/a/3172515/43615

– SuperTempel – 2017-03-29T17:04:59.793

21

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.

TVNshack

Posted 2011-05-12T07:24:25.477

Reputation: 311

Quick FYI: install_name_tool with fail silently if the new path is longer than the path it's replacing. Always verify with 'otool -L' that the path was changed as expected. – user15685 – 2015-08-12T15:31:47.220

Let's say I compile a Plug In (Basically a Dynamic Library by itself) which depends on another Dynamic Library. How should I supply the Plug In and its dependencies for other users? – Royi – 2016-10-28T18:52:38.323

If you instead set DYLD_FALLBACK_FRAMEWORK_PATH, it does work in this case. That's much easier than changing the internal paths of the libs, especially if you need to deliver the built dylibs with your app and can't predict the path they end up at. – SuperTempel – 2017-03-29T17:01:32.890

7

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

Paul Solt

Posted 2011-05-12T07:24:25.477

Reputation: 171