【问题标题】:import hid in python results in error "hid.so: undefined symbol: libusb_open"在 python 中导入 hid 导致错误“hid.so:未定义符号:libusb_open”
【发布时间】:2012-08-27 11:10:12
【问题描述】:

我正在尝试安装 cython-hidapi 以在我的 Ubuntu 12.04 上读取 USB。我按照https://github.com/gbishop/cython-hidapi 的说明安装了以下版本:

  • lib-usb == 1.0.9
  • hidapi == 0.7.0
  • cython == 0.16
  • python == 2.7
  • cython-hidapi == 最新结帐

当我从安装中执行测试部分(python > import hid)时,我收到以下错误:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hid
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python2.7/dist-packages/hid.so: undefined symbol: libusb_open

有谁知道我为什么会收到此错误以及如何检查/处理它?

谢谢! 呜呜呜

【问题讨论】:

  • 运行ldd /usr/local/lib/python2.7/dist-packages/hid.so 以显示链接到 hid.so 的库,如果未找到预期的库,它将报告。然后使用nm 实用程序打印 libusb 库的符号。 nm 输出是否显示存在 libusb_open 符号?
  • ldd 显示如下:ldd /usr/local/lib/python2.7/dist-packages/hid.so linux-vdso.so.1 => (0x00007fff7d5ff000) libpthread.so.0 = > /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb3c88f5000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb3c8538000) /lib64/ld-linux-x86 -64.so.2 (0x00007fb3c8d3b000) nm libusb continas libusb_open: 0000000000003b50 T libusb_open
  • 我猜。 setup.py 运行正常吗?查看github.com/gbishop/cython-hidapi/blob/master/setup.py,可能是LDFLAGS 与您的平台不匹配,因此构建失败。 setup.py 似乎是为 i386 架构设置的,您正在使用 x86-64。尝试更改 setup.py 以匹配您的拱门的库路径。
  • 设置确实,奇怪的是,没有给出任何错误。你对错误的架构是对的。我已经更改并重新安装了该软件包。可悲的是,我仍然收到相同的错误:-(。
  • 这是一个链接器问题。我想 libusb_open 存在于您链接的 libusb 中。但是设置写得不好,并且与usb-1.0明确链接。首先,您必须找到安装在您机器上的 libusb 版本及其所在位置。然后您必须更正 setup.py 中的 include(-I) 和 libs (-L) 路径并运行构建。您还想尝试设置运行时库搜索路径,如下所示: python setup.py --rpath /to_where/libusb_so/lives build 或者在运行 python 之前使用设置它 export LD_LIBRARY_PATH =/path/libusb_so/lives bt 这是一个糟糕的方法。

标签: python usb cython libusb


【解决方案1】:

在过去的一周里,我一直在为同样的问题苦苦挣扎。幸运的是,我的一位非常了解 Cython 世界的朋友能够提供帮助。你需要修改 setup.py 中的 setup(...) 函数如下:

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("hid", ["hid.pyx", "hid-libusb.c"],
                  libraries=["usb-1.0", "udev", "rt"])]
)

我不知道在其他发行版上是否会有细微的差异,但这已经在 Ubuntu 12.04 和 Debian 0.1.12 上进行了测试。使用回复原始帖子的建议来确定正确的链接器标志 (LDFLAGS) 和 libraries= 行。

pull request 已提交给维护者。您也可以通过我的fork 获取零钱。

【讨论】:

  • 链接器(使用gcc)对指定库的顺序很敏感。来自manpageIt makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
  • 您也可以查看layman's explanation
猜你喜欢
  • 2016-02-03
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2017-09-23
  • 2014-10-03
  • 2019-04-19
相关资源
最近更新 更多