0

我使用 Homebrew 在 Mac OS X 上安装了 libusb,位于

/usr/local/地窖/libusb

它的如下所示:

.
└── 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

我有以下代码:

#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;
}

...但编译产生以下结果:

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)

为什么找不到 libusb 库?

4

1 回答 1

4

某处你有一个错误的链接器标志。

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

应该只是:

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

在这种情况下,您不需要指定链接器搜索路径,因此 -L/usr/local/Cellar/libusb/1.0.9/lib不需要。

如果您不想静态链接到该 libusb-1.0.a,而是链接到 .dylib,则需要保留-L/usr/local/Cellar/libusb/1.0.9/lib并替换

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

 -lusb-1.0
于 2013-07-03T20:16:36.007 回答