gpt4 book ai didi

python - 无法运行libclang:错误“找不到指定的模块”

转载 作者:太空狗 更新时间:2023-10-30 02:18:12 26 4
gpt4 key购买 nike

遵循Eli'sglehmann's指南;
(在Windows中)
已从here安装llvm-3.8.0-win32
已安装libclang-py3包(0.3版)
C:\Program Files (x86)\LLVM\bin\libclang.dll添加到我的路径环境变量
当我试图执行下面的代码时(摘自上面提到的指南)

clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin\libclang.dll')

def find_typerefs(node, typename):
# Find all references to the type named 'typename'
if node.kind.is_reference():
ref_node = clang.cindex.Cursor_ref(node)
if ref_node.spelling == typename:
print('Found %s [line=%s, col=%s]' % (
typename, node.location.line, node.location.column))
# Recurse for children of this node
for c in node.get_children():
find_typerefs(c, typename)

index = clang.cindex.Index.create()
tu = index.parse(cppsource)
print('Translation unit:', tu.spelling)
find_typerefs(tu.cursor, 'Person')

我得到以下错误:
C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/Duca/Desktop/PyPlag/rough-scripts/c++_parser_with_libclang.py
Traceback (most recent call last):
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 3623, in get_cindex_library
library = cdll.LoadLibrary(self.get_filename())
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\ctypes\__init__.py", line 425, in LoadLibrary
return self._dlltype(name)
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\ctypes\__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/Duca/Desktop/PyPlag/rough-scripts/c++_parser_with_libclang.py", line 49, in <module>
index = clang.cindex.Index.create()
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 2238, in create
return Index(conf.lib.clang_createIndex(excludeDecls, 0))
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 141, in __get__
value = self.wrapped(instance)
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 3592, in lib
lib = self.get_cindex_library()
File "C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py", line 3628, in get_cindex_library
raise LibclangError(msg)
clang.cindex.LibclangError: [WinError 126] The specified module could not be found. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

Process finished with exit code 1

也试过了,
here下载了cfe-3.8.0.src.tar,并在我的脚本目录中复制了 clang.cindex模块
安装了llvm-3.8.0-win64并更新了path环境变量和 clang.cindex.Config.set_library_path语句(在读取此 question之后)
libclang.dll从安装目录复制到python dlls目录
使用 clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin')clang.cindex.Config.set_library_file('C:\Program Files (x86)\LLVM\bin\libclang.dll')clang.cindex.Config.set_library_file('C:\Program Files (x86)\LLVM\bin')
没有成功。

最佳答案

恐怕你代码中的罪魁祸首是反斜杠。您需要将clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin\libclang.dll')更改为clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll')
另外,我相信这是更好的编程实践,您可以使用os.sep来处理Windows和Linux路径分隔的情况。
另外,代码中还有一个问题;即,您需要将ref_node = clang.cindex.Cursor_ref(node)更改为ref_node = node.get_definition(),以避免获得AttributeError,因为Cursor_ref不再是clang.cindex模块的属性。
在修复上述参数后,运行参数simple_demo_src.cpp Person您应该不会出错,看到这个输出:

Translation unit: simple_demo_src.cpp
Found Person [line=7, col=21]
Found Person [line=13, col=5]
Found Person [line=24, col=5]
Found Person [line=24, col=21]
Found Person [line=25, col=9]

这正是伊莱在他的 page中提到的。

关于python - 无法运行libclang:错误“找不到指定的模块”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36821176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com