python cygwin环境下无法加载clang库

python can't load clang library under cygwin environment

我在windows10下安装了最新的cygwin64,然后开始安装一些cygwin包,比如python3.9和clang 8.0.1,然后通过cygwin终端, pip3 install clang==8.0.1 现在,我正在尝试以下操作:

$ python3 
Python 3.9.10 (main, Jan 20 2022, 21:37:52) 
[GCC 11.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import clang.cindex
>>> index = clang.cindex.Index.create()
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/clang/cindex.py", line 4172, in get_cindex_library
    library = cdll.LoadLibrary(self.get_filename())
  File "/usr/lib/python3.9/ctypes/__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.9/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/clang/cindex.py", line 2698, in create
    return Index(conf.lib.clang_createIndex(excludeDecls, 0))
  File "/usr/local/lib/python3.9/site-packages/clang/cindex.py", line 212, in __get__
    value = self.wrapped(instance)
  File "/usr/local/lib/python3.9/site-packages/clang/cindex.py", line 4146, in lib
    lib = self.get_cindex_library()
  File "/usr/local/lib/python3.9/site-packages/clang/cindex.py", line 4177, in get_cindex_library
    raise LibclangError(msg)
clang.cindex.LibclangError: No such file or directory. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

我的理解是打开一个clang库失败,搜索了一下,在/usr/lib找到了很多库,其中一个是libclang.dll.a,我试了一下根据其他一些 SO 问题建议为其创建一个符号 link,例如:

ln -s /usr/lib/libclang.dll.a /usr/lib/libclang.so.1
ln -s /usr/lib/libclang.dll.a /usr/lib/libclang.so
ln -s /usr/lib/libclang.dll.a /usr/lib/python3.9/libclang.so.1
ln -s /usr/lib/libclang.dll.a /usr/lib/python3.9/libclang.so

但运气不好,因为我不熟悉python。

问题现已解决。根本原因是我安装了每个包的最新版本。所以我安装了 python v3.9 和 clang v8.0.1。 python v3.9 期待 clang v10,它在 cygwin 存储库中不可用,最新的 python clang 绑定是针对版本 v3.7,所以我删除了 python v3.9 和安装的版本 v3.7 需要最新的 clang 版本。

P.S。我删除了这个包,因为它不需要 pip3 安装 clang==8.0.1