user3513915
user3513915

Reputation: 31

How to compile with "-lusb"

How do I compile the code below. They say "compile with -lusb", but I don't know how to do that.

#include <stdio.h>
#include <usb.h>

main(){

    struct usb_bus *bus;
    struct usb_device *dev;
    usb_init();
    usb_find_busses();
    usb_find_devices();
    
    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);
        }
}

Upvotes: 3

Views: 2517

Answers (3)

Owl
Owl

Reputation: 1

gcc -Wall -g -lusb prog.c -o prog

Upvotes: 0

NuclearPeon
NuclearPeon

Reputation: 6059

I was able to successfully compile and run your program.

First, there are three things you should know about -lusb.

  • The - means it's an option to the compiler, in my case I use gcc.
  • The l that prefixes usb means it is an external library we are including in our compilation of this program.
  • The usb is the library name, and is associated with the #include <usb.h> (usb is the usb.h excluding its suffix)

The command I used:

gcc  usb.c -o usb -lusb 


I am using gcc 4.7.3 and libusb 1.0.18

Edit: Correctly specify -lusb after the source code file.

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84569

You need to add -lusb to your gcc command:

gcc -Wall -o usb usb.c -lusb

Upvotes: 1

Related Questions