Question

I've got libusb installed on Mac OS X using Homebrew, and is located at

/usr/local/Cellar/libusb

it's tree looks as follows :

.
└── 1.0.9
    ├── AUTHORS
    ├── COPYING
    ├── ChangeLog
    ├── INSTALL_RECEIPT.json
    ├── NEWS
    ├── README
    ├── TODO
    ├── include
    │   └── libusb-1.0
    │       └── libusb.h
    └── lib
        ├── libusb-1.0.0.dylib
        ├── libusb-1.0.a
        ├── libusb-1.0.dylib -> libusb-1.0.0.dylib
        └── pkgconfig
            └── libusb-1.0.pc

I have the following code:

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

int main(void) {
    puts("USB Test v0.0.1");

    //libusb_device **devices;
    libusb_context *ctx = NULL;
    int result;
    result = libusb_init(&ctx);

    if (result < 0) {
        puts("USB initialization error!");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

...but compilation yields the following:

22:08:26 **** Incremental Build of configuration debug for project usb_test ****
make all 
Building target: usb_test
Invoking: MacOS X C Linker
gcc -L/usr/local/Cellar/libusb/1.0.9/lib -o "usb_test"  ./src/usb_test.o   -l/usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.a
ld: library not found for -l/usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.a
collect2: ld returned 1 exit status
make: *** [usb_test] Error 1

22:08:26 Build Finished (took 68ms)

Why isn't it finding the libusb library?

Was it helpful?

Solution

Somewhere you have a wrong linker flag.

 -l/usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.a

Should just be:

 /usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.a

In this case you do not need to specify the linker search path, so -L/usr/local/Cellar/libusb/1.0.9/lib is not needed.

If you don't want to link statically to that libusb-1.0.a, but link to the .dylib, you need to keep -L/usr/local/Cellar/libusb/1.0.9/lib and replace

 -l/usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.a

with

 -lusb-1.0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top