Compiling against libusb-dev on Ubuntu

2020-06-17 04:04发布

I am trying to compile the example libusb.c provided by libusb package (if you dl the source code.)

It doesn't work to say the least.

#include <stdio.h>
#include <sys/types.h>
#include <libusb/libusb.h>

That causes it to fail, there is no libusb/libusb.h it's usb.h, so I change that. And it fails in new and innovative ways.

I've copied the file over, exactly, and named it example.c

I am using these commands and variations:

gcc -o example example.c -lusb -L /usr/lib/libusb.a
gcc -o example example.c -lusb -L /usr/lib/libusb.so

The errors I get when compiling are:

example.c:25: error: expected ‘)’ before ‘*’ token
example.c: In function ‘main’:
example.c:46: error: ‘libusb_device’ undeclared (first use in this function)
example.c:46: error: (Each undeclared identifier is reported only once
example.c:46: error: for each function it appears in.)
example.c:46: error: ‘devs’ undeclared (first use in this function)

Line 25: static void print_devs(libusb_device **devs)

Line 46: libusb_device **devs;

At first I followed a tutorial, and that failed to compile, in more or less the same ways, so I decided to just try the provided example, and that failed.

Can anyone help me out? Explain what I am doing wrong, cause I am lost on this one.

2条回答
SAY GOODBYE
2楼-- · 2020-06-17 04:47

This is what I had to do on Debian. It should be at least similar in Ubuntu.

Install libusb-1.0-0-dev

Instead of:

#include <libusb/libusb.h>

do:

#include <libusb.h>

Compile with:

gcc example.c `pkg-config --libs --cflags libusb-1.0`
查看更多
The star\"
3楼-- · 2020-06-17 04:53

Just en explanation why your attempt to replace libusb/libusb.h with usb.h fails: usb.h is a header from linux-headers, not from libusb-dev. You need #include <libusb.h> instead.

查看更多
登录 后发表回答