[英]problems compiling c code with libusb on linux
I'm trying to use libusb for a project but i'm unable to get the library working properly. 我正在尝试将libusb用于一个项目,但无法使该库正常工作。 Here is some source code i'm trying to compile. 这是我正在尝试编译的一些源代码。 It doesn't do anything special. 它没有什么特别的。 It's just a dummy program that gets the USB driver list then frees it. 这只是一个虚拟程序,可以获取USB驱动程序列表,然后释放它。
#include <stdio.h>
#include <usb.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
struct libusb_device **devs;
struct libusb_context *context = NULL;
size_t list;
size_t i;
int ret;
ret = libusb_init(&context);
if(ret < 0)
{
perror("libusb_init");
exit(1);
}
list = libusb_get_device_list(context, &devs);
if(list < 0)
{
fprintf(stderr, "error in getting device list\n");
libusb_free_device_list(devs, 1);
libusb_exit(context);
exit(1);
}
libusb_free_device_list(devs, 1);
libusb_exit(context);
return 0;
}
I compile with 我编译
gcc -o test test.c -lusb gcc -o test test.c -lusb
I get the error 我得到错误
/tmp/cc2hwzii.o: in function 'main:
test.c:(.text+0x24): undefined reference to 'libusb_init'
test.c:(.text+0x59): undefined reference to 'libusb_get_device_list'
test.c:(.text+0x8e): undefined reference to 'libusb_free_device_list'
test.c:(.text+0x9f): undefined reference to 'libusb_exit'
collect2: error: ld returned 1 exit status
I'm running ubuntu 14.04.3 I've installed libusb by sudo apt-get install libusb-dev I've searched for my header file and it is called usb.h I've looked to make sure I have the correct flag and it's -lusb 我正在运行ubuntu 14.04.3我已经通过sudo apt-get install libusb-dev安装了libusb我搜索了我的头文件,并将其称为usb.h。我一直在寻找以确保我拥有正确的标志,并且它是-lusb
any ideas? 有任何想法吗? I'd appreciate the help. 非常感谢您的帮助。 If any more information is needed just ask. 如果需要更多信息,请询问。
those libusb_init
are included in libusb-1.0
. 这些libusb_init
包含在libusb-1.0
。 you have to install libusb-1.0-0-dev 您必须安装libusb-1.0-0-dev
$ sudo apt-get install libusb-1.0-0-dev
$ gcc -o test test.c -lusb-1.0
/usr/lib/x86_64-linux-gnu/pkgconfig/libusb.pc
which is included in libusb-dev
says that the version is 0.1.12 libusb-dev
包含的/usr/lib/x86_64-linux-gnu/pkgconfig/libusb.pc
表示版本为0.1.12
and /usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc
which is included in libusb-1.0-0-dev
says that the version is 1.0.17. libusb-1.0-0-dev
包含的/usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc
表示版本为1.0.17。
http://www.libusb.org/ says that 0.1 is legacy, and seems that API is different from 1.0. http://www.libusb.org/表示0.1是旧版,似乎API与1.0不同。
You forgot to include the file that defines the functions, such as libusb_init. 您忘记了包含定义功能的文件,例如libusb_init。 Have you tried including libusb.h? 您是否尝试过包含libusb.h?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.