无法将libusb链接到Mac上的C项目

时间:2013-07-03 20:12:21

标签: c eclipse macos eclipse-cdt libusb

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

的/ usr /本地/地窖/ 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库?

1 个答案:

答案 0 :(得分:3)

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

 -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

在这种情况下,您不需要指定链接器搜索路径,因此

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

-L/usr/local/Cellar/libusb/1.0.9/lib

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