2

I know that similar questions have been asked many times, but I could not find a solution that worked in my case. I am a newbie with macOS, and for sure I'm missing something on how dynamic linking works in Mac. I must import a dylib library in python, which in turn should import another library. Here are the relevant files, environment variables and parts of the code:

$ echo $DYLD_LIBRARY_PATH 
/usr/local/lib/:/Developer/NVIDIA/CUDA-10.2/lib

$ ls /Developer/NVIDIA/CUDA-10.2/lib/libcurand.*
/Developer/NVIDIA/CUDA-10.2/lib/libcurand.10.dylib
/Developer/NVIDIA/CUDA-10.2/lib/libcurand.dylib

$ ls -al /usr/local/lib/libcurand.*
lrwxr-xr-x  1 golosio  admin  50 Feb 24 09:55 /usr/local/lib/libcurand.10.dylib -> /Developer/NVIDIA/CUDA-10.2/lib/libcurand.10.dylib
lrwxr-xr-x  1 golosio  admin  47 Feb 24 09:55 /usr/local/lib/libcurand.dylib -> /Developer/NVIDIA/CUDA-10.2/lib/libcurand.dylib

$ ls -al /usr/local/lib/libneurongpu.*
-rwxr-xr-x  1 root  admin  4496 Feb 24 10:32 /usr/local/lib/libneurongpu.0.dylib
lrwxr-xr-x  1 root  admin    20 Feb 24 10:32 /usr/local/lib/libneurongpu.dylib -> libneurongpu.0.dylib
-rwxr-xr-x  1 root  admin   953 Feb 24 10:32 /usr/local/lib/libneurongpu.la

$ python

>>> import ctypes

>>> lib_path="/usr/local/lib/libneurongpu.dylib"

>>> _neurongpu=ctypes.CDLL(lib_path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 366, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/lib/libneurongpu.dylib, 6): Library not loaded: @rpath/libcurand.10.dylib
  Referenced from: /usr/local/lib/libneurongpu.dylib
  Reason: image not found

I would like to understand not only what I should do to import the libraries, but also why what I am doing is not working.

2 Answers 2

2

libneurongpu is calling a sub library libcurand via an @rpath and not finding it wherever it’s looking. Try an otool -l /usr/local/lib/libneurongpu.0.dylib to see the @rpath. Then you may wish to adjust the library to call the sub library from the correct location.

There are two options.

1. Remove the @rpath in the library path. install_name_tool -change @rpath/libcurand.10.dylib libcurand.10.dylib /usr/local/lib/libneurongpu.0.dylib This will search the same directory as the calling library.

2. Set the @rpath (or add another) to the correct directory. install_name_tool -add_rpath /usr/local/bin /usr/local/lib/libneurongpu.0.dylib

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Indeed I solved my problem in this way. I have found a good article that helped me to understand how dynamic libraries are imported in macOS here: blog.krzyzanowskim.com/2018/12/05/rpath-what
2

The correct answer is the one posted by Richard Barber. For anyone who has similar problems and wants to understand how dynamic link libraries are imported in macOS, a simple and good article can be found here: https://blog.krzyzanowskim.com/2018/12/05/rpath-what/

1 Comment

I'm glad it worked out. In most situations, the install_names are taken care of by an automaton, especially if they are numerous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.