0
votes

i use eclipse ide for c project on mac computer i install usblib sudo brew install libusb and my program

#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
int main(void) {
    libusb_context **libcontext;
    int status = libusb_init(libcontext);
    if (status == 0) {
        printf("success");
    }
}

the libusb is at enter image description here

when i build error

10:17:53 **** Build of configuration Debug for project testusb **** make all Building file: ../src/testusb.c Invoking: Cross GCC Compiler gcc -O0 -g3 -Wall -c -fmessage-length=0 -stdlib=libstdc++ -MMD -MP -MF"src/testusb.d" -MT"src/testusb.o" -o "src/testusb.o" "../src/testusb.c" ../src/testusb.c:16:6: warning: unused variable 'status' [-Wunused-variable] int status = libusb_init(libcontext); ^ ../src/testusb.c:16:27: warning: variable 'libcontext' is uninitialized when used here [-Wuninitialized] int status = libusb_init(libcontext); ^~~~~~~~~~ ../src/testusb.c:15:29: note: initialize the variable 'libcontext' to silence this warning libusb_context libcontext; ^ = NULL 2 warnings generated. Finished building: ../src/testusb.c Building target: testusb Invoking: Cross GCC Linker gcc -L/opt/local/lib -o "testusb" ./src/testusb.o Undefined symbols for architecture x86_64:
"_libusb_init", referenced from: _main in testusb.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: *
[testusb] Error 1

how to fix that?

enter image description here

1
It seems the library is not linked. Check your project setting.MikeCAT
i add the setting picture,but i don't know how to set it.this is my first use c programAmitābha

1 Answers

0
votes

thanks @Fang suggestion Libusb undefined reference to i solve the problem. first type

pkg-config --list-all

query all installed libraries,check libusb whether installed, then type

pkg-config --libs libusb

link against the libusb i want to use

there should be the output

-lusb

then right click project properties->c/c++ Build->Setting->Cross GCC Linker Miscellaneous,type -lusb-1.0