I am attempting to compile third party code to a shared library so I can call it from Python using ctypes. This is my first time knowingly working with shared libraries.
I build the object code using:
gcc -c -fPIC -o elisa3-lib.o ../elisa3-lib.c
gcc -c -fPIC -o usb-comm.o ../usb-comm.c
Then I attempt to build the shared library:
gcc -shared -fPIC -Wl,-install_name,libelisa3.so -o libelisa3.so.1.0 elisa3-lib.o usb-comm.o -lc
And I get linker errors because usbcomm.c references libusb.h:
#ifdef __APPLE__
#include </opt/local/include/libusb-1.0/libusb.h>
#endif
With errors:
Undefined symbols for architecture x86_64:
"_libusb_bulk_transfer", referenced from:
_usb_send in usb-comm.o
_usb_receive in usb-comm.o
"_libusb_claim_interface", referenced from:
_openCommunication in usb-comm.o
"_libusb_close", referenced from:
_closeCommunication in usb-comm.o
"_libusb_exit", referenced from:
_closeCommunication in usb-comm.o
"_libusb_init", referenced from:
_openCommunication in usb-comm.o
"_libusb_open_device_with_vid_pid", referenced from:
_find_nrf_device in usb-comm.o
"_libusb_release_interface", referenced from:
_closeCommunication in usb-comm.o
ld: symbol(s) not found for architecture x86_64
How do I link correctly? Do I need to compile libusb to a shared library as well and link with -l?
"Undefined symbols" are functions/variables that the linker can't find. In short, yes you need to include libusb as either one of the libraries or objects you're linking in. Do whichever is easier or more appropriate.