Creating a program that uses libusb on ubuntu
$ gcc -c testUsb.c
$ gcc testUsb.o testUsb -lusb
Normally, I think you can build like this, but If I try to make this a shared library (.so) instead of an executable file, I get a build error.
$ gcc -c testUsb.c
$ gcc -shared -fPIC testUsb.o -o libTestUsb.so -lusb
/usr/bin/ld: testUsb.o: relocation R_X86_64_PC32 against symbol `devInfo' can not be used when making a shared object。 -Recompile with fPIC.
/usr/bin/ld:Last link failed: bad value
collect2: error: ld returned 1 exit status
What is this? saw it for the first time.
The content of the error is "libusb was not built with -fPIC, so it cannot be used in this context. If you want to use it, please rebuild with -fPIC".
No, I wouldn't have to rebuild libusb for this.
I tried various things, but all of them failed (in the direction of not rebuilding libusb), so I don't think it is necessary to write in detail here, but the final solution was to build as follows.
$ gcc -shared -fPIC testUsb.c -o libTestUsb.so -lusb
Compile and link directly from .c files without creating .o files. Now that you have a .so file that works fine without any errors, what's the difference with generating an .o file? ..
I don't understand what I'm doing normally ... Now I have to find out why this works.
Recommended Posts