无法运行libclang:error''指定的模块无法找到'' [英] Cannot run libclang: error ''specified module could not be found''

查看:4122
本文介绍了无法运行libclang:error''指定的模块无法找到''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eli's glehmann's 指南;



(在Windows中)


已安装 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文件(x86)\LLVM\bin\libclang.dll')

def find_typerefs(node,typename):
#查找名为'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))
#为节点的子节点
为节点中的c递归。 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')


$ b b

我得到以下错误:

  C:\Users\Duca\AppData\Local\程序\Python\Python35-32\python.exe C:/ Users / Duca / Desktop / PyPlag / rough-scripts / c ++ _ parser_with_libclang.py 
回溯(最近一次调用):
文件C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py,第3623行,在get_cindex_library
library = cdll.LoadLibrary(self.get_filename())
文件C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\ lib \ctypes\__init __。py,第425行,在LoadLibrary中
return self._dlltype(name)
文件C:\Users\Duca\AppData\Local\Programs \Python\Python35-32\lib\ctypes\__init __。py,第347行,在__init__
self._handle = _dlopen(self._name,mode)
OSError:[WinError 126]找不到指定的模块

在处理上述异常时,发生另一个异常:

跟踪(最近最后调用):
文件 C:/ Users / Duca / Desktop / PyPlag / rough-scripts / c ++ _ parser_with_libclang.py,第49行,在< module>
index = clang.cindex.Index.create()
文件C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\\ \\ site-packages \clang \cindex.py,行2238,在create
中返回索引(conf.lib.clang_createIndex(excludeDecls,0))
文件C:\Users \ Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py,第141行,位于__get__
value = self.wrapped (instance)
文件C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\lib\site-packages\clang\cindex.py ,line 3592,in lib
lib = self.get_cindex_library()
文件C:\Users\Duca\AppData\Local\Programs\Python\Python35-32\\ \\ lib \site-packages\clang\cindex.py,第3628行,在get_cindex_library
raise LibclangError(msg)
clang.cindex.LibclangError:[WinError 126]指定的模块不能被发现。要提供libclang的路径,请使用Config.set_library_path()或Config.set_library_file()。

过程完成退出代码1




cfe-3.8.0.src.tar /download.htmlrel =nofollow>此处,并将我的脚本目录中的 clang.cindex 模块复制



安装了 LLVM-3.8.0-win64 并更新了路径环境变量以及 clang.cindex.Config.set_library_path 语句(在阅读此问题之后)



将安装目录中的 libclang.dll 复制到我的Python DLL dir



使用 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') 到 c code> clang.cindex.Config.set_library_file('C:/ Program Files(x86)/LLVM/bin/libclang.dll') p>

或者,我相信这是更好的编程实践,您可以使用 os.sep 来处理Windows和Linux路径分隔符。



此外,你的代码;也就是说,您需要将 ref_node = clang.cindex.Cursor_ref(node) 更改为 c $ c $ ref_node = node.get_definition ),以避免得到 AttributeError ,因为 Cursor_ref 不再是 clang.cindex 模块。



修复上述操作后,使用参数 simple_demo_src运行。 cpp Person 你应该没有错误,看到这个输出:

 翻译单位:simple_demo_src.cpp 
Found person [line = 7,col = 21]
Found Person [line = 13,col = 5]
Found Person [line = 24,col = 5] Person [line = 24,col = 21]
Found person [line = 25,col = 9]


$ b b

这正是Eli对他的页。


Following Eli's and glehmann's guides;

(in Windows)

installed LLVM-3.8.0-win32 from here

installed libclang-py3 package (version 0.3)

added C:\Program Files (x86)\LLVM\bin\libclang.dll to my Path environment variables

When I try to execute the code below (taken from the guides I mention above)

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

I get the following error:

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

Also tried,

downloaded cfe-3.8.0.src.tar from here and copied the clang.cindex module inside my script's directory

installed LLVM-3.8.0-win64 and updated the Path environment variable as well as the clang.cindex.Config.set_library_path statement (after reading this question)

copied the libclang.dll from the installation dir to my Python DLLs dir

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

with no success.

解决方案

I am afraid the main culprit in your code is the backward slash. You need to change the clang.cindex.Config.set_library_path('C:\Program Files (x86)\LLVM\bin\libclang.dll') to clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll').

Alternatively, and I believe it is better programming practice, you could use the os.sep to handle both Windows and Linux path separator cases.

Also, there is another issue in your code; namely, you need to change the ref_node = clang.cindex.Cursor_ref(node) to ref_node = node.get_definition() in order to avoid getting an AttributeError since Cursor_ref is no longer an attribute of the clang.cindex module.

After fixing the above, running with the parameters simple_demo_src.cpp Person you should be getting no errors and seeing this output:

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]

which is exactly what Eli mentions on his page.

这篇关于无法运行libclang:error''指定的模块无法找到''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆