我想開始使用libclang與Python。我試圖讓一個示例代碼(http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/)到的Windows工作,這裏是我試圖運行的代碼的一部分:爲什麼這個python腳本找不到libclang dll?
#!/usr/bin/python
# vim: set fileencoding=utf-8
import sys
import os
import clang.cindex
import itertools
...
print("Setting clang path")
# I tried multiple variations. Libclang is correctly installed in the specified location.
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin')
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin/libclang.dll')
# I also tried moving the dll into the Python installation folder.
clang.cindex.Config.set_library_file('C:/Python27/DLLs/libclang.dll')
print("Clang path set")
index = clang.cindex.Index.create()
...
我扯下了代碼的其他部分,但如果它們相關,我可以發佈它們。該生產線
index = clang.cindex.Index.create()
拋出以下錯誤:
Setting clang path
Clang path set
Traceback (most recent call last):
File "D:\libclangtest\boost_python_gen.py", line 60, in <module>
index = clang.cindex.Index.create()
File "D:\libclangtest\clang\cindex.py", line 2095, in create
return Index(conf.lib.clang_createIndex(excludeDecls, 0))
File "D:\libclangtest\clang\cindex.py", line 141, in __get__
value = self.wrapped(instance)
File "D:\libclangtest\clang\cindex.py", line 3392, in lib
lib = self.get_cindex_library()
File "D:\libclangtest\clang\cindex.py", line 3423, in get_cindex_library
raise LibclangError(msg)
clang.cindex.LibclangError: [Error 193] %1 is not a valid Win32 application. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().
,這是什麼原因呢?我是否設置了dll路徑錯誤?我嘗試了多種方式,使用了前景和反斜槓,我也嘗試將DLL移出Program Files以使路徑不包含空格,但沒有任何工作。
我是一個初學libclang和Python的初學者,如果我要求一些微不足道的東西。
確保python和libclang都是32位或64位。另外,請確保您的libclang.dll的路徑位於PATH環境變量中。 –
我將它添加到PATH中,但我不認爲這是問題,因爲我必須用set_library_file函數手動設置路徑。我要檢查它是32位還是64位,謝謝你的提示! –