ld: symbol(s) not found for architecture x86_64 (libusb)

josef.van.niekerk picture josef.van.niekerk · Nov 25, 2013 · Viewed 8.8k times · Source

I'm trying to compile the following libusb snippet on my Mac:

#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>

int main(void) {
    libusb_device **devices;
    ssize_t device_count = 0;

    device_count = libusb_get_device_list(NULL, &devices);

    printf("%d devices found\n", (int)device_count);

    return EXIT_SUCCESS;
}

I have libusb installed via Homebrew.

I'm getting the following error during compilation:

ld: symbol(s) not found for architecture x86_64

The full compiler output is as follows:

22:28:24 **** Incremental Build of configuration Debug for project libusb ****
make all 
Building file: ../src/libusb.c
Invoking: Cross GCC Compiler
gcc -I/usr/local/Cellar/libusb/1.0.9/include/libusb-1.0/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/libusb.d" -MT"src/libusb.d" -o "src/libusb.o" "../src/libusb.c"
Finished building: ../src/libusb.c

Building target: libusb
Invoking: Cross GCC Linker
gcc  -o "libusb"  ./src/libusb.o   
Undefined symbols for architecture x86_64:
  "_libusb_get_device_list", referenced from:
      _main in libusb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libusb] Error 1

22:28:24 Build Finished (took 119ms)

I understand the problem is to do with the linker not finding the libusb library, right? How do I tell the compiler where that is in Eclipse CDT?

enter image description here

Answer

Charlie Burns picture Charlie Burns · Nov 25, 2013

As we worked out in all the comments the link command that worked is

gcc -L/usr/local/Cellar/libusb/1.0.9/lib -o "libusb_example" ./src/libusb_example.o -lusb1.0