在pynq-z2想在其中引入C语言进行调用,首先要生成.so文件
一系列问题如下
OSError: : decode.so:unexpected reloc type 0x03
用新的编译工具使用-fPIC(-fpic -pie)选项重新编译相关依赖库文件,依赖库目标文件避免出现TEXTREL标识。
gcc decode.cpp -shared -fPIC -o decode.so
又出错
AttributeError: ./decode.so: undefined symbol: YOLOdecode
ldd -r decode.so
查看缺少什么东西
我把exp函数的math.h删了,自己写了近似的函数
double exp(float x)
{
x = 1.0 + x / 256.0;
x *= x; x *= x; x *= x; x *= x;
x *= x; x *= x; x *= x; x *= x;
return x;
}
nm -D decode.so
查看含有什么函数
知道函数名字是_Z10YOLOdecodePA13_A13_fPA6_fRi。
import ctypes
lib = ctypes.cdll.LoadLibrary("./decode.so")
但是又有问题
ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
改一下输入数据格式
lib._Z10YOLOdecodePA13_A13_fPA6_fRi.argtypes = [ctypes.c_float, ctypes.c_float,ctypes.c_int]
还是有问题
ArgumentError: argument 1: <class 'TypeError'>: wrong type
改为数组空间
在ctypes中,基于前述的基本数据类型,还可以构建自定义的数组类型,自定义数组类型的方法: 新类型名 = ctypes基本类型名*长度n,赋值方法:对象名=新类型名(赋值1,赋值2……赋值n) ;
lib._Z10YOLOdecodePA13_A13_fPA6_fRi.argtypes = [ctypes.c_float*125*13*13, ctypes.c_float*845*6,ctypes.c_int]
ArgumentError: argument 1: <class 'TypeError'>: expected c_float_Array_125_Array_13_Array_13 instance instead of PynqBuffer
哎,没解决