【问题标题】:Linking against libusb on Ubuntu doesn't work在 Ubuntu 上链接 libusb 不起作用
【发布时间】:2014-06-26 07:42:36
【问题描述】:

我们正在尝试在需要 libusb 的 Ubuntu 机器上编译一些代码,但当它尝试链接它时,我们不断收到“未定义的引用”错误。

有问题的代码是这个: https://gitorious.org/minibox-dcdcusb/minibox-dcdcusb

它需要 libusb,所以我安装它:

apt-get install libusb libusb-dev

然后我运行 make 并得到以下错误:

➜  minibox-dcdcusb git:(master) ✗ make                    
cc -Wall  main.c -o dcdc-usb `pkg-config --libs libusb` -lm -L. -ldcdc-usb
./libdcdc-usb.so: undefined reference to `usb_get_driver_np'
./libdcdc-usb.so: undefined reference to `usb_interrupt_read'
./libdcdc-usb.so: undefined reference to `usb_find_busses'
./libdcdc-usb.so: undefined reference to `usb_find_devices'
./libdcdc-usb.so: undefined reference to `usb_set_configuration'
./libdcdc-usb.so: undefined reference to `usb_interrupt_write'
./libdcdc-usb.so: undefined reference to `usb_control_msg'
./libdcdc-usb.so: undefined reference to `usb_set_altinterface'
./libdcdc-usb.so: undefined reference to `usb_init'
./libdcdc-usb.so: undefined reference to `usb_set_debug'
./libdcdc-usb.so: undefined reference to `usb_open'
./libdcdc-usb.so: undefined reference to `usb_claim_interface'
./libdcdc-usb.so: undefined reference to `usb_detach_kernel_driver_np'
./libdcdc-usb.so: undefined reference to `floor'
./libdcdc-usb.so: undefined reference to `usb_get_busses'
collect2: error: ld returned 1 exit status
make: *** [dcdc-usb] Error 1

我已验证 pkg-config 报告了正确的配置参数:

➜  minibox-dcdcusb git:(master) ✗ pkg-config --libs libusb
-lusb  

而且我已经确保二进制文件在它们应该在的位置:

➜  minibox-dcdcusb git:(master) ✗ ls -l /usr/lib/x86_64-linux-gnu/libusb*
lrwxrwxrwx 1 root root    37 des  3 13:58 /usr/lib/x86_64-linux-gnu/libusb-0.1.so.4 -> /lib/x86_64-linux-gnu/libusb-0.1.so.4
-rw-r--r-- 1 root root 42998 des  3 13:58 /usr/lib/x86_64-linux-gnu/libusb.a
lrwxrwxrwx 1 root root    41 des  3 13:58 /usr/lib/x86_64-linux-gnu/libusb.so -> /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4

而且我什至确保链接器似乎抱怨的引用实际上在 libusb 二进制文件中:

(剧透:链接器抱怨的所有符号似乎都在那里)

最后,我使用 -Wl,--verbose 运行编译命令,以查看链接器获取的确切二进制文件,看起来它实际上正在获取正确的二进制文件,但它无法链接他们。这是它的输出:

我对这里可能出现的问题感到很迷茫?为什么链接器不能链接到 libusb?

【问题讨论】:

  • gcc 的参数顺序很重要。如果将 pkg-config 调用放在源参数和输出参数之前,它会起作用吗?
  • 不...我已经尝试过了,现在再次验证它没有任何效果。事实上,我在某处读到链接器信息应该出现在 需要该库的源参数之后。
  • 我永远不记得哪个订单是我只知道它有时会导致问题。实际上我只是注意到这是在抱怨库中的符号而不是二进制文件。您可能需要 -Wl,-E 来从二进制文件中导出符号,因为您没有动态链接 libusb。

标签: ubuntu gcc linker makefile ld


【解决方案1】:

为什么链接器不能链接到 libusb?

它可以,但是当链接器看到该库时,它没有对 libusb 中符号的任何未定义引用,所以它只是继续前进并忽略它。后来链接器看到了libdcdc-usb,它对libusb中的符号有未定义的引用,但是到那时已经太晚了,链接器已经关闭了libusb并停止查看它。

正如评论所说,链接器参数的顺序很重要,但评论建议将它们提前移动,这是错误的方式。

事实上,我在某处读到链接器信息应该出现在需要该库的源参数之后。

不仅仅是“源文件之后的链接器参数”,您需要对库进行排序,以便提供符号定义的库位于需要这些定义的对象之后。

libdcdc-usb 依赖于libusb,所以libusb 必须稍后,所以将pkg-config 调用移到末尾。

【讨论】:

  • 该死的太棒了!我也必须移动 -lm 但之后它就可以工作了!谢谢!
  • 那是因为其他库依赖于libm 而它不依赖于它们。
猜你喜欢
  • 2016-11-09
  • 2017-04-10
  • 2014-02-10
  • 1970-01-01
  • 2018-01-03
  • 2019-12-17
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
相关资源
最近更新 更多