无法运行libclang:错误'''指定的模块无法找到''

时间:2016-04-24 09:06:59

标签: python clang libclang

关注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')

没有成功。

1 个答案:

答案 0 :(得分:6)

我担心代码中的主要罪魁祸首是反斜杠。您需要将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的属性{1}}模块。

修复上述内容后,使用参数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]

这正是Eli在page上提到的。