Python development efficiency, but the company project code is written in C language in the past, so decided to original code compiled into so library, then python to invoke,
For this specially written a set of python and c test code, investigate the feasibility of python call c,
Test code: Python will set some functions to callback function in C language, C code can convenient call,
Steps:
1, C code using GCC compiler as so file;
2, use the Python ctypes library, call so file,
Running on the PC (x86 architecture), in the Windows and Linux system run smoothly,
Conclusion: code no grammar problems, memory, the management of the pointer is correct
So I had to cross compiler, to compile C code into Openwrt ARM kernel hardware can run so file
Then running python code, the same result as long as the operation to call python callback function in C language, will be submitted to the Segmentation fault.
Which the great spirit experience in this respect, still hope some directions,
Attach a python code:
The import ctypes
The from ctypes import *
Def PyCoreCB (a) :
Print (' CallBack Core - {} '. The format (a))
Return 5
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Def BeginTry () :
Ll=ctypes. CDLL. LoadLibrary
Lib=ll ("./PyObjArm. So ")
# lib=ll ("./PyObj. So ")
CMPCORE=CFUNCTYPE (c_int c_int)
CCoreFunc=CMPCORE (PyCoreCB)
Try:
Lib. ZExeCB. Restype=c_int
Lib. ZExeCB (cCoreFunc)
Except the Exception as e:
Print (' Error: {} '. The format (repr (e)))
Print (" -- -- -- -- -- -- -- -- -- print Test -- -- -- -- -- -- -- -- ')
BeginTry ()
Attach the C code:
# include & lt; stdio.h>
# include & lt; Stdlib. H>
# include & lt; String. H>
Typedef int (* cb_core) (int a);
Int ZExeCB cb_core (df)
{
If (df!=NULL)
{
Printf (" % s ", "the Exec Main CB2 \ r \ n");
Df (2);
}
Return 5;
}
CodePudding user response:
https://stackoverflow.com/questions/37953055/segmentation-fault-when-using-ctypes-and-threading