About half a month ago, a file summarizing the wrapper module of python.h came out, so I will post it. By the way, it was created for raspberry pi3.
The C language has a header called python.h, which can be used for implementation. The program creates a function in c language and calls the function in python by a wrapper module called python wrapper.
In python2 and 3, it was different than I imagined, so I will post the creation method of each. There is no difference in c language functions.
Even if you look at the sample program, you will see "What? What is this?" .. .. Lol I will write down what I understood as an additional note at the end as much as possible, so I hope you will see it if you are interested after seeing the sample program. Also, the environment this time is raspberry pi3. Functions and wrapper modules are separate files. (The same file is easier and easier)
・ C language functions
#include <iostream>
void numPrint(int num){
std::cout << num << std::endl
}
compile $ g++ -Wall -pthread -fpic -o intPrint.o -c intPrint.cpp -lpigpiod_if2 -lrt
・ Python2.7
#include <Python.h>
extern void numPrint(int num);
PyObject *numPrint(PyObject* self, PyObject* args){
int num;
if(!PyArg_ParseTuple(args, "i", &num)) return NULL;
numPrint(num);
return Py_Buildvalue("");
}
static PyMethodDef numPrintmethods[]={
{"numPrint",numPrint ,METH_VARARGS},
{NULL,NULL,0},
};
PyMODINIT_FUNC initPWM(void){
Py_InitModule("numPrint",numPrintmethods);
}
compile $ g++ -fpic -I/usr/include/python2.7 -o intPrintWrapper.o -c intPrintWrapper.cpp -lpigpiod_if2 -lrt $ g++ -shared intPrint.o intPrintWrapper.o -o intPrintmodule.so -lpigpiod_if2 –lrt
・ Python3.4
#include <Python.h>
#include <cstdio>
extern void numPrint(int num);
PyObject *numPrint(PyObject* self, PyObject* args){
int num;
if(!PyArg_ParseTuple(args, "i", &num)) return NULL;
numPrint(num);
return Py_Buildvalue("");
}
static PyMethodDef numPrintmethods[]={
{"numPrint",numPrint ,METH_VARARGS ,NULL},
{NULL,NULL, 0, NULL},
};
static PyModuleDef numPrintmodule = {
PyModuleDef_HEAD_INIT,
"numPrint",
NULL,
-1,
numPrintmethods
};
PyMODINIT_FUNC PyInit_numPrint(void){
return PyModule_Create(&numPrintmodule);
}
compile $ arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c intPrintWrapper.cpp -o intPrintWrapper.o $ arm-linux-gnueabihf-g++ -pthread -shared intPrint.o -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 intPrintWrapper.o -o intPrint.cpython-34m.so -lpigpiod_if2 -lrt (* I think some of them are unnecessary. ・ Please be careful about the file name and folder location.)
○ Addition ・ Simple table of contents About PyObject / About PyModuleDef / Make modules available elsewhere / Implement in the same file / Implement in class /
● About PyObject PyObject hasn't changed in python version (I think)
-In this article, there is only one argument, but in other cases, With no arguments
PyObject *numPrint(PyObject* self, PyObject* args){
numPrint();
return Py_Buildvalue("");
}
Two arguments
PyObject *numPrint(PyObject* self, PyObject* args){
int num, num2;
if(!PyArg_ParseTuple(args, "ii", &num, &num2)) return NULL;
numPrint(num, num2);
return Py_Buildvalue("");
}
It becomes.
・ About arguments
if(!PyArg_ParseTuple(args, “ii”, &num, &num2)) return NULL; "Ii" is a state where there are two int type arguments in C language.
・ About the return value The return value can be returned by return.
● Make the module usable in other places (move folder) $ sudo mv intPrint.cpython-34m.so /usr/local/lib/python3.4/dist-packages/ intPrint.cpython-34m.so
● Implementation in the same file You can. I haven't done it.
● Implementation in class You can. I haven't done it.
When I was examining the wrapper module myself, I couldn't find it created in a separate file, so I'm posting it. In the first place, I think it is rare to create a separate file with python.h (laughs).
Finally, thank you for reading this far. I hope it helps someone even a little.
Recommended Posts