Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling against libusb-dev on Ubuntu

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.

like image 285
J. Martin Avatar asked Oct 12 '11 13:10

J. Martin


2 Answers

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`
like image 172
Piotr Praszmo Avatar answered Nov 20 '22 11:11

Piotr Praszmo


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.

like image 7
jpalecek Avatar answered Nov 20 '22 11:11

jpalecek