我正在尝试将第三方代码编译到共享库,因此我可以使用ctypes从Python调用它。这是我第一次有意识地使用共享库。
我使用以下方法构建目标代码:
gcc -c -fPIC -o elisa3-lib.o ../elisa3-lib.c
gcc -c -fPIC -o usb-comm.o ../usb-comm.c
然后我尝试构建共享库:
gcc -shared -fPIC -Wl,-install_name,libelisa3.so -o libelisa3.so.1.0 elisa3-lib.o usb-comm.o -lc
我得到了链接器错误,因为usbcomm.c引用了libusb.h:
#ifdef __APPLE__
#include </opt/local/include/libusb-1.0/libusb.h>
#endif
有错误:
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
如何正确链接?我是否还需要将libusb编译为共享库并与-l?
链接答案 0 :(得分:1)
“未定义的符号”是链接器无法找到的函数/变量。简而言之,是的,您需要将libusb作为您链接的库或对象之一。包括哪个更容易或更合适。