Henk Schurink
Henk Schurink

Reputation: 429

gcc - undefined reference to `libusb_init'

I'm trying to compile a piece of code which uses libusb:

#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <assert.h>

int main(void) {
    libusb_context *context = NULL;
    int rc = 0;

    rc = libusb_init(&context);
    assert(rc == 0);

    libusb_exit(context);
    return 0;
}

Upon compiling with gcc -lusb -lusb-1.0 sample.c -o sample the following errors emerge:

/tmp/ccr65JBT.o: In function `main':
sample.c:(.text+0x2e): undefined reference to `libusb_init'
sample.c:(.text+0x62): undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status

To make sure libusb is availible on my system:

raven@enforcer:~/sample$ pkg-config --libs libusb-1.0
-lusb-1.0
raven@enforcer:~/sample$ pkg-config --libs libusb
-lusb

I'm running Ubuntu 18.04 with gcc 7.3.0-16ubuntu3, how to fix?

Upvotes: 5

Views: 8186

Answers (1)

Henk Schurink
Henk Schurink

Reputation: 429

Got it working by appending the flags after the .c: gcc sample.c -o sample -lusb -lusb-1.0

Autotools: sample_LDADD instead of sample_LDFLAGS

Thanks to Felix Palmen.

Upvotes: 5

Related Questions