Libusb undefined引用

Res*_*shi 9 c++ linux linker libusb

我正在尝试在我的操作系统上设置libusb API.我在libusb.org上下载了libusb api.我遵循标准安装程序:

cd into directory
./configure
make
make check //without errors
make install
Run Code Online (Sandbox Code Playgroud)

然后我启动了Eclipse C/C++并从互联网上的教程中复制了一些代码.但是在尝试构建它时,我得到了以下输出:

main.cpp:(.text+0x19): undefined reference to `libusb_init'
main.cpp:(.text+0x76): undefined reference to `libusb_set_debug'
main.cpp:(.text+0x8a): undefined reference to `libusb_get_device_list'
main.cpp:(.text+0x136): undefined reference to `libusb_free_device_list'
main.cpp:(.text+0x142): undefined reference to `libusb_exit'
/tmp/ccOWJGwe.o: In function `printdev(libusb_device*)':
main.cpp:(.text+0x162): undefined reference to `libusb_get_device_descriptor'
main.cpp:(.text+0x28a): undefined reference to `libusb_get_config_descriptor'
main.cpp:(.text+0x4d4): undefined reference to `libusb_free_config_descriptor'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我在/ lib中有libusb.so,我在/ usr/local/include中有usb.h,在/ usr/local/lib中有.so和libusb.a的链接.

代码中的#include也是正确的.

我知道问题出现在链接器中但是我有点不能使它工作:)

我正在使用Fedora 15操作系统和gcc 4.6.0 20110603(Red Hat 4.6.0-10)版本编译器.

那么我该怎么办才能解决这些未定义的引用呢?非常感谢你的帮助:)

小智 23

我确实遇到了同样的问题.但我能够通过向链接器添加"-lusb-1.0"来解决它.

例如:g ++ myfile.cpp -lusb-1.0

  • 而不是接受的答案对我有用. (2认同)

Xtr*_*oce 16

您必须在链接器中设置库链接器标志以进行编译,您可以通过执行在控制台中获取完整列表

pkg-config --list-all
Run Code Online (Sandbox Code Playgroud)

这些是您在系统上安装的库,您必须链接到要使用的库.所以在你的例子中它是libusb所以你这样做

pkg-config --libs libusb
Run Code Online (Sandbox Code Playgroud)

应该有输出

-lusb
Run Code Online (Sandbox Code Playgroud)

这为您提供了必须传递给链接器的标志.

然后编辑项目的配置并搜索链接标志,在构建选项中的某处应该有一个文本字段.我不是很害羞在哪里找到它,但谷歌搜索它建议:

-lusb-1.0
Run Code Online (Sandbox Code Playgroud)

找到它之后,只需在文本字段中添加链接器标志即可.