ld: symbol(s) not found for architecture x86_64 (libusb) - c

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?

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

Related

linker command failed while trying to compile .c file with g++

Just posted a simple c code and gotten this error message. can anyone explain where the problem could be?
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mac#MACs-MacBook-Pro-2 C % cd "/Users/mac/C/" && g++ tes.c++ -o tes && "/Users/mac/C/"tes
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
you have to compile with gcc and not g++, g++ is to compile .cpp or .cc files
gcc tes.c++ -o tes

Eclipse C compiler throwing errors

I was trying to compile helloworld.c in Eclipse Oxygen.1 on Mac 10.12.6
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Hello World!");
return 0;
}
and it throws such an error:
20:41:02 **** Incremental Build of configuration Debug for project HelloWorld ****
make all
Building target: HelloWorld
Invoking: Cross GCC Linker
gcc -o "HelloWorld" ./helloworld.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [HelloWorld] Error 1
20:41:03 Build Finished (took 938ms)
I am quite sure that the problem isn't the code itself, but what?

eclipse build error in c project

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
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?
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

How do I include the object files for openssl/md5 library?

I have included the openssl/md5.h in my source.
#include <openssl/md5.h>
This is my (part of) my code:
char *pf_generate_pfdhr_string(int firstfree, int numpages)
{
const int md5_digest_len = 16;
char hash[md5_digest_len];
MD5_CTX md5_ctx;
MD5_Init(&md5_ctx);
int hdr_arr[2] = {firstfree, numpages};
MD5_Update(&md5_ctx, hdr_arr, 2*sizeof(int));
MD5_Final(hash, &md5_ctx);
return hash;
}
And this is my output
ranlib build/libpf.a
cc -g -O2 -Wall -Wextra -Isrc -rdynamic tests/pf_tests.c build/libpf.a -o tests/pf_tests
Undefined symbols for architecture x86_64:
"_MD5_Final", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
"_MD5_Init", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
"_MD5_Update", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.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: *** [tests/pf_tests] Error 1
I have included the header file so the compiler knows the function exists. However, it seems unable to find the object files for the md5 library. How can I include it in my build?
If I am using Make, what is the best way to do so?
Thank you.
Check the documentation for your platform. It may be -lcrypto -lssl. It may not be. If your platform supports pkg-config, use pkg-config --libs openssl.

Eclipse build configuration for OpenMP

I am trying to learn OpenMP, starting with the following simple snippet
#include <stdio.h>
#include <stdlib.h>
int main(void) {
#pragma omp parallel
printf("Hello OpenMP!\n");
return 0;
}
Simply compiling from the command line works:
cls ~/Desktop $ gcc -fopenmp HelloOpenMP.c -o HelloOpenMP
cls ~/Desktop $ ./HelloOpenMP
Hello OpenMP!
Hello OpenMP!
However, I'd like to use Eclipse with CDT. I created a new build configuration "OpenMP" and tried to add the -fopenmp flag under "Miscellaneous", copying the other settings from the "Debug" build configuration.
The build fails with
14:56:16 **** Incremental Build of configuration OpenMP for project HelloOpenMP ****
make all
Building file: ../src/HelloOpenMP.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"src/HelloOpenMP.d" -MT"src/HelloOpenMP.d" -o "src/HelloOpenMP.o" "../src/HelloOpenMP.c"
Finished building: ../src/HelloOpenMP.c
Building target: HelloOpenMP
Invoking: MacOS X C Linker
gcc -o "HelloOpenMP" ./src/HelloOpenMP.o
Undefined symbols for architecture x86_64:
"_GOMP_parallel_end", referenced from:
_main in HelloOpenMP.o
"_GOMP_parallel_start", referenced from:
_main in HelloOpenMP.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [HelloOpenMP] Error 1
So I guess this was not the right place to add the -fopenmp compiler option? What configuration should I use to build with OpenMP?
Add -fopenmp flag to the linker section as well.

Resources