Reputation: 7844
I'm trying the Python binding for clang. I installed LLVM and its python bindings using homebrew
on Mac OS X Maverics with command line
brew install llvm --with-clang --with-python --with-lld
The loading code is
import clang
import clang.cindex
clang.cindex.Config.set_library_path('/usr/local/Cellar/llvm/3.5.0/lib')
index = clang.cindex.Index.create()
But this throws an error:
clang.cindex.LibclangError: dlopen(/usr/local/Cellar/llvm/3.5.0/lib/libclang.dylib, 6): Library not loaded: @rpath/libLLVM-3.5.dylib Referenced from: /usr/local/Cellar/llvm/3.5.0/lib/libclang.dylib Reason: image not found. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().
But I don't understand why this error occurs. Doesn't @rpath
here refers to /usr/local/Cellar/llvm/3.5.0/lib
? But there is a file called libLLVM-3.5.dylib
under that directory. Why does this loading cause an error and how to fix it?
Upvotes: 6
Views: 9339
Reputation: 3354
A robust alternative is to use Config.set_library_path
, works great in interactive Python (IPython, Jupyter) too.
from clang.cindex import Index,Config,CursorKind
# check where is LLVM installed on your machine (here on MacOS)
Config.set_library_path('/usr/local/Cellar/llvm/16.0.3/lib')
SCRIPT_PATH = './tcpdump/print-ppp.c'
# C99 is a proper compiler for tcpdump, as per their docs
index = Index.create()
translation_unit = index.parse(SCRIPT_PATH, args=['-std=c99'])
This is also recommended in docs.
Upvotes: 2
Reputation: 8266
Elevating @synthesizerpatel's comment to an answer:
Add the following environment variable to your environment:
DYLD_LIBRARY_PATH=/usr/local/Cellar/llvm/3.5.0/lib/
The readme also mentions
You may need to alter
LD_LIBRARY_PATH
so that the Clang library can be found.
Upvotes: 2