Linux Libusb 未定义的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7050482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:42:01  来源:igfitidea点击:

Libusb undefined reference to

c++linuxlinkerlibusb

提问by Reshi

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

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

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

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:

然后我启动了 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

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.

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

Also the #include inside the code is correct.

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

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

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

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

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

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

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

采纳答案by Xtroce

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

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

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

pkg-config --libs libusb

there should be the output

应该有输出

-lusb

or

或者

-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.

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

EDIT

编辑

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

由于我的答案是公认的,我还添加了另一个似乎对很多人有用的标志。

回答by Delan Azabani

What is your linker command line? You need to have -lusbin the linking command; just having the header included won't work.

你的链接器命令行是什么?你需要-lusb在链接命令中有;仅包含标题是行不通的。

回答by emboss

I don't use Eclipse C/C++ but I am pretty sure the reason is the same that I faced some while ago when setting up a C project in Netbeans.

我不使用 Eclipse C/C++,但我很确定原因与我不久前在 Netbeans 中设置 C 项目时遇到的原因相同。

It's not enough to have the #includein your code and the library at the right location - you also have to tell Eclipse where to look for them and how to use them. This turorialshows you how to set it up in Eclipse.

#include将代码和库放在正确的位置是不够的- 您还必须告诉 Eclipse 在哪里查找它们以及如何使用它们。本教程向您展示了如何在 Eclipse 中设置它。

回答by user3240675

I did face the same problem. But I was able to solve it by adding '-lusb-1.0' to the linker.

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

e.g : g++ myfile.cpp -lusb-1.0

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