【问题标题】:Why can't this python script find the libclang dll?为什么这个 python 脚本找不到 libclang dll?
【发布时间】:2014-05-08 23:36:16
【问题描述】:

我想开始使用 libclangPython。我正在尝试获取示例代码 (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位,谢谢提示!

标签: python clang libclang


【解决方案1】:

@SK-logic 评论说我应该检查 Python 和 libclang 是 32 位还是 64 位。 Libclang 是 32 位的,但是我找不到检查我的 Python 安装是 32 还是 64 的方法,所以我重新安装了 32 位版本,现在它可以工作了。所以问题可能是我有 64 位版本的 Python。

【讨论】:

  • 可以在Dependency Walker(depends.exe)中打开EXE,查看是32位还是64位。
  • 您可以通过多种方式检查CPython是32位还是64位,例如platform.architecture()[0]sys.version 字符串(MSC 构建); sys.maxsize2**31-1 还是 2**63-1;或者通过struct.Struct('P').sizectypes.sizeof(ctypes.c_void_p),指针大小是4 字节还是8 字节。
【解决方案2】:

我遇到了类似的问题(Windows 7 x64、Anaconda3 x64)。使用

import clang.cindex
clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')

解决了这个问题。请注意,您需要使用 slashes(不是反斜杠),并指定 bin/libclang.dll 的路径(而不是 lib/libclang.dll)。

【讨论】:

  • 我有类似的问题:我可以找到 libclang.dll 但是,当我尝试创建索引时,我收到一个错误,提示找不到函数 clang_CXXRecord_isAbstract。我有 python 3.6 64 位、anaconda3 64 位和 windows 10,显然是 64 位。 Llvm 由 anaconda 安装。 Python-clang 是 6.0.0.2,clang --version 输出 3.9.1。我很困惑。
猜你喜欢
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 2020-12-14
  • 1970-01-01
相关资源
最近更新 更多