Libusb and how to use its packages in Ubuntu

15,867

Solution 1

Have a look at http://packages.debian.org/wheezy/i386/libusb-dev/filelist: The file you want to include is usb.h. Also, you'll have to tell the compiler where it can find the compiled library functions: Add -lusb to the compiler command line to make it load libusb.so.

Solution 2

Try including it like so:

#include <libusb-1.0/libusb.h>

and then compile it like so:

g++ main.cpp -o main -lusb-1.0

Solution 3

Actually at least in Debian 7.4 (wheezy), and probably in Ubuntu also, there are two distinct libusb packages: libusb-dev (0.1.12-20+nmu1) and libusb-1.0-0-dev (1.0.11-1). Confusingly, they can both be installed concurrently and provide header files in different locations:

$ dpkg -L libusb-dev|grep /usr/include
/usr/include
/usr/include/usb.h
$ dpkg -L libusb-1.0-0-dev|grep /usr/include
/usr/include
/usr/include/libusb-1.0
/usr/include/libusb-1.0/libusb.h

Solution 4

Try #include <usb.h>. The "lib" is part of the Linux naming convention, i.e. library "foo" has header foo.h and is called libfoo-dev in the Debian package structure, and linked as -lfoo, and the compiled library files are called libfoo.a and libfoo.so.

Share:
15,867
Amjad
Author by

Amjad

I am a student, doing BSc(Hons) Software development in Lancaster University, Lancaster UK

Updated on June 13, 2022

Comments

  • Amjad
    Amjad almost 2 years

    I have installed libusb by using the following command. I am not sure if it was right or not and the command was

    sudo apt-get install libusb-dev
    

    Once I have installed (and I am not sure if it has installed or not because I am a novice user of Ubuntu), I want to know how would I use the library, because I write some sample code which uses <libusb.h>, but when I compile that C++ file using

    g++ test_libusb.cpp
    

    that throws the following error,

    test_libusb.cpp:2:20: fatal error: libusb.h: No such file or directory compilation terminated.

    I am clueless what to do. I can't find any source on the Internet to get to the bottom of this...

    I want to know two things here:

    1. How do I add the libusb library in C/C++ so I can use <libusb.h>?
    2. What would some sample code be? Only a few lines to see if libusb is working...
  • thejh
    thejh about 11 years
    It's linked using -lfoo, not -libfoo.
  • Amjad
    Amjad about 11 years
    as i said i am a novice if you see that page stackoverflow.com/questions/14722083/… in above example they have used libusb example as i mentioned '#incluce <libusb.h>'
  • thejh
    thejh about 11 years
    @UsmanSharifAmjadKhan: Well, at least on debianish systems, the include file is named usb.h, not libusb.h, so you'll need to use #include <usb.h>.
  • Loligans
    Loligans over 7 years
    This should be the accepted answer. I use Fedora 24 and libusb is located at /usr/include/libusb-1.0/libusb.h adding the '-1.0' to '-lusb-1.0' fixed everything