libusb 未定义的引用 [英] Libusb undefined reference to

查看:85
本文介绍了libusb 未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to set up libusb API on my OS. I downloaded libusb api on libusb.org. I followed the standard installation procedure:

cd into directory
./configure
make
make check //without errors
make install

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

Then I launched Eclipse C/C++ and copied some code from the tutorial found on the internet. But when trying to build it I got following output:

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

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

I have libusb.so in /lib and also I have usb.h in /usr/local/include and the link for the .so and libusb.a in /usr/local/lib.

代码里面的#include也是正确的.

Also the #include inside the code is correct.

我知道问题出在链接器中,但我有点无法让它工作:)

I know that problem is in linker but I, kind of, cannot make it work :)

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

I'm using Fedora 15 operating system and gcc 4.6.0 20110603 (Red Hat 4.6.0-10) version compiler.

那么我能做些什么来解决这些未定义的引用呢?非常感谢您的帮助:)

So what could I do to resolve these undefined references? Thanks very much for help :)

推荐答案

你必须在链接器中设置库链接器标志以进行编译,您可以通过执行

you have to set the library linker flag for compilation in the linker, you can get a full list in the console by executing

pkg-config --list-all

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

These are the libraries which you have installed on your system and you have to link against the ones you want to use. so in your example it is libusb so you do

pkg-config --libs libusb

应该有输出

-lusb

-lusb-1.0

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

This gives you the flag you have to pass to the linker. e.g.

g++ myfile.cpp -lusb[-1.0]

然后您编辑项目的配置并搜索链接器标志,构建选项中的某处应该有一个文本字段.我不太确定在哪里可以找到它,但建议使用谷歌搜索:

Then you edit the configuration of the project and search for the linkerflags, there should be a textfield for that somewhere in the buildoptions. i'm not quite shure where to find it but googling for it suggested:

Project -> Properties -> C/C++
Build -> Miscellaneous -> flags

找到后,只需在文本字段中添加链接器标志就可以了.

After you found it, just add the linker flag in the textfield and you should be fine.

编辑

因为我的答案是被接受的,所以我还添加了另一个似乎对很多人都有效的标志.

since my answer is the accepted one, I also added the other flag that seems to work for a lot of people.

这篇关于libusb 未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆