2016-04-24 69 views
3

正在關注Eli'sglehmann's指南;無法運行libclang:錯誤''指定的模塊找不到''

(在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

當我嘗試執行下面的代碼(從導遊採取我上面提到)

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 

也試過,

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

沒有成功。

+0

你試過set_library_file而不是set_library_path了嗎? –

+0

是的,完全一樣的錯誤。 :( –

+2

它看起來像你有幾個可能的問題。1)你不知道爲什麼DLL被發現; 2)你不確定你是否有一組兼容的Python綁定。 python綁定(其中任何一個)都將包含足夠的功能來執行你所希望的功能(所以這不是問題) - 我對此很有信心,因爲實現基於ctypes。如果這留下的DLL問題,你可以手動編輯cindex.py文件打印準確打印哪個文件,如果你還'斷言os.path.exists'你可能會更深入瞭解什麼是錯誤 –

回答

5

恐怕你的代碼中的罪魁禍首是反斜槓。您需要將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提及。

+1

需要這麼多,謝謝! 'os.sep'是很好的建議。並感謝你拿起'AttributeError'。 –

相關問題