All about programming in GNU/LINUX

LIBUSB-1:Intro to LIBUSB , Writing and compiling the Hello World LIBUSB code in Ubuntu

Hello Friends ……..Recently a tiny bug in the Linux USB Device Driver had crashed my kernel , Its very common that bugs in the Device Drivers written brings down the system by crashing the kernel.The reason being that  unlike the usual programs we write Drivers run in kernel space.When programs run in user space and when we commit common programming snags like segmentation faults  or any invalid memory reference the kernel handles it to maintain the system stability , but in case of drivers which run in kernel space there is no one to monitor  since the kernel forms the core of the OS hierarchy .

Because of the above reasons its not safe to code in kernel space and better not unless there are no alternative ways to achieve the purpose.In driver writing , A simple unnoticed bug in a line of code could compromise the whole system.

Recently i came across a open source project by name LIBUSB . At first glance i assumed LIBUSB to be not so handy library to use and program with , but when i realized that using LIBUSB Linux USB Drivers can be written completely from user space i was amused to start using it to write USB Device Drivers under LINUX. 

What is LIBUSB ???

The official site quotes that “Libusb is a C library that gives applications easy access to USB devices on many different operating systems. libusb is an open source project, the code is licensed under the GNU LESSER GENERAL PUBLIC LICENSE or later.”

We’ll after realizing the importance of LIBUSB i was very curious on starting off with my first USB driver using LIBUSB libraries. So i installed LIBUSB libraries by issuing the following command on the terminal of my KUBUNTU machine

sudo apt-get install libusb-dev

we’ll the first of installing the LIBUSB library is over , now i wrote my HELLO WORLD libusb driver .

#include <stdio.h>
#include <stdlib.h>
#include <usb.h>
int main(){
    printf("Hello world!\n");
    return 0;

Writing the hello world LIBUSB program was a piece of cake , a trivial task 😀 But the smile didnt remain longer since i couldnt find a way to compile the code 😦

During compiling the code using gcc/g++ the LIBUSB library had to be linked by using -L option . But i couldnt find the libusb libraries under /usr/lib where usually all the libraries reside.All the online resources i  referred stated the libraries to reside in /usr/lib .

Later i found out that the path where LIBUSB libraries (.a) and headers and their names differ with few versions of LIBUSB . So the best way out was to search where the current LIBUSB package installed its headers and library.

Since Debain based distributions (ubuntu , kubnutu , MINT ) use .deb packages which gets installed using the `dpkg` mechanism , i tried the following command to know the list of directories used by the libusb-dev package

dpkg -L libusb-dev

Now focus on the last few lines of the output and find the path where the library libusb.a and the header file usb.h/libusb.h is installed.


Now make sure that you add the header name in the code and  , during compilation just add the parent directory of along with the -L option of gcc/g++ .Also dont miss to add -lusb option during compilation.

gcc basiclusb.c -o basic -L/usr/lib/x86_64-linux-gnu/ -lusb


This succesfully compiles the Hello World LIBUSB code to give out the executable 😀 I hope this post would come pretty handy for those who want to start of with Writing USB device drivers in Linux using LIBUSB libraries.Catch u folks with more insights on wrting USB drivers under LINUX using LIBUSB in the posts to come 🙂





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s