I need to list existing USB hub and the devices connected in the hub using my C++ program.

I can able to print the USB Hub and devices connected in hub from terminal using the commands

lsusb -v 

I want to use that feature in my C++ program.

How I can do this programmatically. Is there any C++ classes available to use in my Qt application.

closed as off topic by Gilles, Michael Mrozek Jan 17 '13 at 0:23

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • use libusb, search here: there was a similar question recently. Look here. – Eddy_Em Jan 16 '13 at 11:45
  • In archlinux this is in libusb-compat package. – Eddy_Em Jan 17 '13 at 7:41

Here the simplest analogue of lsusb:

#include <stdio.h>
#include <usb.h>
    struct usb_bus *bus;
    struct usb_device *dev;
    for (bus = usb_busses; bus; bus = bus->next)
        for (dev = bus->devices; dev; dev = dev->next){
            printf("Trying device %s/%s\n", bus->dirname, dev->filename);
            printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
            printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);

Compile with -lusb

  • I tried it but did not worked to me. I am using Ubuntu OS and qt creator and I try after installing libusb-1.0-0-dev, libusb++-0.1-4c2 etc. where this usb.h header file come from – Arun Kumar K S Jan 17 '13 at 7:13
  • Finally i installed its libary file.Now I can include #include <usb.h> But where its library file is located? – Arun Kumar K S Jan 17 '13 at 11:39
  • @arun kumar KS at 11:39 , So where did you find the library file? – qqqqq Sep 14 '16 at 16:38
  • I am not remembering but check this link. this may help you..github.com/libusb/libusb/issues/17 – Arun Kumar K S Sep 20 '16 at 7:10

This is Fedora, for Debian based distributions you should be able to do something similar:

  • Get the package containing that program: "rpm -qif /usr/bin/lsusb" tells you it comes from the usbutils package, homepage is http://www.linux-usb.org
  • Look at the sources posted there (presumably in C, it should be easy to extract what you need from there)


  • Find out where/how the kernel publishes the information on the USB setup, i.e. /sys/bus/usb/devices shows the whole setup
  • Read up on how that information is organized (this is set up for easy parsing, so it shouldn't be too hard to handle)

A desperate solution:

  • Just run the lsusb(8) program itself and get the required data from its output

BTW, consider using Python instead of C++ to write your GUI application. GUIs rarely are performance critical enough, and the savings in programmer time more than offset any efficiency losses (and the user probably won't notice them anyway).

Not the answer you're looking for? Browse other questions tagged or ask your own question.