[英]LIBUSB compile error
I am running c/c++ on Ubuntu and trying to compile the following code 我在Ubuntu上运行c / c ++并尝试编译以下代码
#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>
int main() {
libusb_context *context = NULL;
libusb_device **list = NULL;
int rc = 0;
ssize_t count = 0;
rc = libusb_init(&context);
assert(rc == 0);
count = libusb_get_device_list(context, &list);
assert(count > 0);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = list[idx];
libusb_device_descriptor desc = {0};
rc = libusb_get_device_descriptor(device, &desc);
assert(rc == 0);
printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
}
}
I get the following error when after I compile my code. 编译代码后,出现以下错误。 Don't have any idea what should I do?
不知道该怎么办?
/tmp/ccESLZ0k.o: In function `main':
libusbtest.cpp:(.text+0x2f): undefined reference to `libusb_init'
libusbtest.cpp:(.text+0x64): undefined reference to `libusb_get_device_list'
libusbtest.cpp:(.text+0xd4): undefined reference to `libusb_get_device_descriptor'
collect2: ld returned 1 exit status
I am a novice user of Ubuntu, c/c++ and libusb, so any help would be appreciated 我是Ubuntu,c / c ++和libusb的新手用户,因此对您有所帮助
Thanks 谢谢
This is a linker error. 这是一个链接器错误。
You need to tell the linker to include libusb
, which contains these referenced functions (eg -lusb
) and also where it is (eg -L/usr/local/lib
). 您需要告诉链接器包括
libusb
,其中包含这些引用的函数(例如-lusb
)以及它的位置(例如-L/usr/local/lib
)。 Actual values will depend on your installation. 实际值取决于您的安装。
As Avidanborisov's answer highlights, you can use the pkg-config tool to determine the linker flags. 正如Avidanborisov的答案突出显示的那样,您可以使用pkg-config工具确定链接器标志。 On my system this looks like:
在我的系统上,这看起来像:
% pkg-config --libs libusb-1.0
-L/usr/local/Cellar/libusb/1.0.9/lib -lusb-1.0
You can feed this information directly to gcc : 您可以直接将此信息提供给gcc :
% g++ libusbtest.cpp $(pkg-config --libs libusb-1.0) -o libusbtest
Assuming all goes according to plan, you should now have an executable file libusbtest
in your current working directory. 假设一切都按计划进行,那么您现在应该在当前工作目录中具有一个可执行文件
libusbtest
。 You can run it like this: 您可以这样运行它:
% ./libusbtest
Vendor:Device = 05ac:8006
Vendor:Device = 05ac:8006
Vendor:Device = 05ac:8005
Vendor:Device = 05ac:8005
Vendor:Device = 05ac:850a
Vendor:Device = 05ac:023f
Vendor:Device = 05ac:8403
使用pkg-config
获取该库所需的编译器标志:
g++ libusbtest.cpp `pkg-config --libs libusb` -o libusbtest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.