在某些嵌入式的情况下,需要用Python仿真,并基于C实现算法。为了调试方便,需要将一些C携程的代码编译成DLL供Python调用。这里写点本人踩的坑吧。
1、Visual Studio调用lib的库是静态库。动态库是DLL。记得在属性中切换。
2、大部分Python都是64位,所以记得改成Debug X64
3、对于打算暴露的方法,需要加点ZAR_EXPORTS。如下所示:
#ifdef ZAR_EXPORTS #define ZAR_EXPORTS __declspec(dllexport) #else #define ZAR_EXPORTS __declspec(dllimport) #endif ZAR_EXPORTS bool funcA(int filter_num, float sample_hz, float cutoff_hz); ZAR_EXPORTS void funcB(int filter_num, float input[], float output[]); ZAR_EXPORTS void funcC();
4、对于编译好的dll,可以用visual studio自带的dumpbin工具进行检测。方法见其他博文,此处略。
python调用dll示例:
_path = "A.dll" _findpeak = ctypes.cdll.LoadLibrary(_path) _findpeak.peak_cnt_init.argtypes = [c_int,c_float,c_int,c_int,c_float,c_float] _findpeak.peak_cnt_update.argtypes = [c_float,c_int] _findpeak.peak_cnt_update.restype = [c_float] #argtype是入参,restype是回参



