简体   繁体   English

LIBUSB编译错误

[英]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 tool to determine the linker flags. 正如Avidanborisov的答案突出显示的那样,您可以使用工具确定链接器标志。 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 : 您可以直接将此信息提供给

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

相关问题 八进制常量错误(libusb) - octal constant error (libusb) libusb_open 在 windows 10 上返回 LIBUSB_ERROR_NOT_SUPPORTED - libusb_open returns LIBUSB_ERROR_NOT_SUPPORTED on windows 10 libusb_open在jni android中返回LIBUSB_ERROR_ACCESS - libusb_open return LIBUSB_ERROR_ACCESS in jni android 如何解决ros中的&#39;libusb的未定义引用&#39;错误? - How to solve the 'undefined reference to libusb' error in ros? libusb:错误[submit_bulk_transfer]提交失败错误-1 errno = 2 - 我对LibUSB做错了什么? - libusb: error [submit_bulk_transfer] submiturb failed error -1 errno=2 - What am I doing wrong with LibUSB? 使用Libusb 1.0发送中断传输会返回LIBUSB_ERROR_IO,但在接收时不会 - Sending a interrupt transfer using Libusb 1.0 returns an LIBUSB_ERROR_IO but not when receiving libusb_get_string_descriptor_ascii()超时错误? - libusb_get_string_descriptor_ascii() timeout error? 错误:无效使用非静态成员函数&#39;int test :: hotplug_callback(libusb_context *,libusb_device *,libusb_hotplug_event,void *)&#39; - error: invalid use of non-static member function ‘int test::hotplug_callback(libusb_context*, libusb_device*, libusb_hotplug_event, void*)’ 没有候选错误编译错误 - no candidates for error compile error /usr/include/libfreenect.hpp:33:错误:libusb.h:没有这样的文件或目录 - /usr/include/libfreenect.hpp:33: error: libusb.h: No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM