Compiling against libusb-dev on Ubuntu

J. Martin picture J. Martin · Oct 12, 2011 · Viewed 19.3k times · Source

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.

Answer

Piotr Praszmo picture Piotr Praszmo · Oct 12, 2011

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`