Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libclang cannot find libclang.dylib #75

Open
SteveBronder opened this issue Oct 17, 2024 · 0 comments
Open

libclang cannot find libclang.dylib #75

SteveBronder opened this issue Oct 17, 2024 · 0 comments

Comments

@SteveBronder
Copy link

Given the simple program below libclang is not able to find cmath while making the translation unitj

// test.cpp
#include <cmath>

int main() {
  return 1;
}

The script below and a fresh virtual environment gives an error when attempting to parse the translation unit. It cannot find the location of libclang.dylib.

import clang.cindex
filename = "./test.cpp"
comp_args = [
  "-std=c++17",
]  # Add any necessary compiler flags here
translation_unit = clang.cindex.TranslationUnit.from_source(filename, comp_args)
Traceback (most recent call last):
  File "[...]/venv/lib/python3.13/site-packages/clang/cindex.py", line 3949, in get_cindex_library
    library = cdll.LoadLibrary(self.get_filename())
  File "/opt/homebrew/Cellar/[email protected]/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ctypes/__init__.py", line 471, in LoadLibrary
    return self._dlltype(name)
           ~~~~~~~~~~~~~^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ctypes/__init__.py", line 390, in __init__
    self._handle = _dlopen(self._name, mode)
                   ~~~~~~~^^^^^^^^^^^^^^^^^^
OSError: dlopen(libclang.dylib, 0x0006): tried: 'libclang.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibclang.dylib' (no such file), '/opt/homebrew/lib/libclang.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/lib/libclang.dylib' (no such file), '/opt/homebrew/lib/libclang.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/lib/libclang.dylib' (no such file), '/usr/lib/libclang.dylib' (no such file, not in dyld cache), 'libclang.dylib' (no such file)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<python-input-0>", line 7, in <module>
    translation_unit = clang.cindex.TranslationUnit.from_source(filename, comp_args)
  File "[...]/venv/lib/python3.13/site-packages/clang/cindex.py", line 2991, in from_source
    index = Index.create()
  File "[...]/venv/lib/python3.13/site-packages/clang/cindex.py", line 2877, in create
    return Index(conf.lib.clang_createIndex(excludeDecls, 0))
                 ^^^^^^^^
  File "[...]/venv/lib/python3.13/site-packages/clang/cindex.py", line 224, in __get__
    value = self.wrapped(instance)
  File "[...]/venv/lib/python3.13/site-packages/clang/cindex.py", line 3922, in lib
    lib = self.get_cindex_library()
  File "[...]/venv/lib/python3.13/site-packages/clang/cindex.py", line 3956, in get_cindex_library
    raise LibclangError(msg)
clang.cindex.LibclangError: dlopen(libclang.dylib, 0x0006): tried: 'libclang.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibclang.dylib' (no such file), '/opt/homebrew/lib/libclang.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/lib/libclang.dylib' (no such file), '/opt/homebrew/lib/libclang.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/lib/libclang.dylib' (no such file), '/usr/lib/libclang.dylib' (no such file, not in dyld cache), 'libclang.dylib' (no such file). To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

Manually adding in the location of libclang.dylib allows the translation unit to compile, but gives diagnostic warnings that cmath could not be found

import clang.cindex
clang.cindex.Config.set_library_path("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/")
filename = "./test.cpp"
comp_args = [
  "-std=c++17",
]  # Add any necessary compiler flags here
print("args: ", comp_args)
translation_unit = clang.cindex.TranslationUnit.from_source(filename, comp_args)
for diag in translation_unit.diagnostics:
    print("Diagnostic:\n")
    print(diag)
./test.cpp:1:10: fatal error: 'cmath' file not found

Then attempting to manually add the resource directory still does not allow libclang to find the correct cmath

clang++ -print-resource-dir
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16
import clang.cindex
clang.cindex.Config.set_library_path("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/")
filename = "./test.cpp"
comp_args = [
  "-std=c++17",
  "-resource-dir",  "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16"
]  # Add any necessary compiler flags here
print("args: ", comp_args)
translation_unit = clang.cindex.TranslationUnit.from_source(filename, comp_args)
for diag in translation_unit.diagnostics:
    print("Diagnostic:\n")
    print(diag)
args:  ['-std=c++17', '-resource-dir', '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16']
Diagnostic:

./test.cpp:1:10: fatal error: 'cmath' file not found

System information

Software:

    System Software Overview:

      System Version: macOS 14.7 (23H124)
      Kernel Version: Darwin 23.6.0
      Boot Volume: Macintosh HD

Hardware:

    Hardware Overview:

      Model Name: MacBook Pro
      Model Identifier: Mac14,5
      Chip: Apple M2 Max
      System Firmware Version: 11881.1.1
xcode-select -v
xcode-select version 2408.
clang++ --version
Apple clang version 16.0.0 (clang-1600.0.26.3)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
pip freeze
clang==17.0.6
libclang==18.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant