3

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

josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157
  • 2
    Yes, somehow you need to add -lusb to your link command. Maybe also a -L switch to tell it where to find the libusb. I don't use eclipse so I can't tell you how. Just a comment: it's a little confusing that your .c file is named "libusb". A project that uses libusb would generally not be called "libusb". – Charlie Burns Nov 25 '13 at 20:39
  • I agree on the project name, it's just a sample. It'll probably get binned when I get to code my actual implementation. I'll look into adding the -L and -lusb in Eclipse, at least I know what's missing from the command line gcc invocations. – josef.van.niekerk Nov 25 '13 at 20:46
  • Command now looks like this: gcc -o "libusb" ./src/libusb.o -l/usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.dylib, as I've added libusb-1.0.dylib to Eclipse CDT config. Now getting: ld: library not found for -l/usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.dylib – josef.van.niekerk Nov 25 '13 at 21:04
  • Try gcc -o libusb ./src/libusb.o /usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.dylib – Charlie Burns Nov 25 '13 at 21:08
  • If you are giving gcc the path to the lib you don't want a switch in front of it. Just the path to the .dylib file. – Charlie Burns Nov 25 '13 at 21:09
  • Thing is I'm using Eclipse, and it pretty much wants to use the -l – josef.van.niekerk Nov 25 '13 at 21:26
  • I've added a screenshot on the Eclipse Gross GCC Linker > Libraries page. – josef.van.niekerk Nov 25 '13 at 21:28
  • And as you can see, I tried adding the .a library as well...neither seems to work. – josef.van.niekerk Nov 25 '13 at 21:29
  • -I tells gcc where to look for include files. -L takes a path to a directory to look for libraries. If you are specifying the library on the command line, you just list it like an input file. No flags. – Charlie Burns Nov 25 '13 at 21:29
  • Hi Charlie, got Eclipse calling this now: gcc -L/usr/local/Cellar/libusb/1.0.9/lib -o "libusb_example" ./src/libusb_example.o -llibusb-1.0.a – josef.van.niekerk Nov 25 '13 at 21:30
  • -l specifies a library for when you don't include the path to it. eg -lusb goes off and looks for libusb.{a,dylib,so} in the libary search paths. – Charlie Burns Nov 25 '13 at 21:31
  • Try -lusb-1.0 instead of -llibusb-1.0.a – Charlie Burns Nov 25 '13 at 21:33
  • You fixed it Charlie! Could you please post an answer so I can accept it! ;) Thanks for the assistance! – josef.van.niekerk Nov 25 '13 at 21:35

1 Answers1

4

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
Charlie Burns
  • 6,994
  • 20
  • 29
  • I had the same problem, but I use macports. (And it is 7 years later.) A slight modification of this answer, using "-l" too, worked: `cc -I/opt/local/include -L/opt/local/lib/ -lusb-1.0 myprog.c -o myprog` – Matt May 03 '20 at 15:53