Call c language from python (python.h)

Introduction

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.

Method

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.

python version

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.

About the sample program

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)

Sample program

・ 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.)

Postscript

○ 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 20180306_1.JPG

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.

in conclusion

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

Call c language from python (python.h)
Call C from Python with DragonFFI
Call popcount from Ruby / Python / C #
Call C / C ++ from Python on Mac
Call C language functions from Python to exchange multidimensional arrays
Generate C language from S-expressions in Python
Call your own C language shared library from Python using ctypes
Call a Python script from Embedded Python in C ++ / C ++
C language to see and remember Part 1 Call C language from Python (hello world)
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
Call CPLEX from Python (DO cplex)
[Python] How to call a c function from python (ctypes)
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Call Matlab from Python to optimize
Call a Python function from p5.js.
Introduction to Protobuf-c (C language ⇔ Python)
Tips for calling Python from C
Execute Python code from C # GUI
Run Python scripts synchronously from C #
Call python from nim with Nimpy
Go language to see and remember Part 8 Call GO language from Python
Closure 4 language comparison (Python, JavaScript, Java, C ++)
Use C ++ functions from python with pybind11
Call a command from Python (Windows version)
sql from python
python C ++ notes
python, openFrameworks (c ++)
MeCab from Python
Create a C array from a Python> Excel sheet
Writing logs to CSV file (Python, C language)
Wrap C with Cython for use from Python
Call Python library for text normalization from MATLAB
Wrap C ++ with Cython for use from Python
I want to make C ++ code from Python code!
Call Polly from the AWS SDK for Python
An easy way to call Java from Python
Touch MySQL from Python 3
Operate Filemaker from Python
Try to make a Python module in C language
Python: Natural language processing
Python C / C ++ Extension Pattern-Pointer
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Let's call your own C ++ library with Python (Preferences)
C language ALDS1_3_B Queue
Use MySQL from Python
Run python from excel
Introduction to Python language
Execute command from Python
Python points from the perspective of a C programmer
Next Python in C
Use MySQL from Python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Call your own python module from the ROS package
Use BigQuery from python.
[C language algorithm] Endianness