【发布时间】:2014-05-08 23:36:16
【问题描述】:
我想开始使用 libclang 和 Python。我正在尝试获取示例代码 (http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/) 以在 Windows 上运行,这是我尝试运行的代码的一部分:
#!/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位,谢谢提示!