Note: The following articles are out of date.
Consider using pybind11.
[Python acceleration] c ++ built-in by pybind11
"Speeding up Python by fusing with Cython C" From O'Reilly I tried to trace on Ubuntu that a function written in C ++ can be imported and executed in Python. First of all, since the sample of this book is on github, download it in a zip file. https://github.com/cythonbook/examples Move to the extracted directory. examples-master/08-wrapping-cxx/01-simple-example-mt_rng-class
Source code written in C ++ mt19937.h mt19937.cpp
Declaration of external C code in Cython (extension .pxd) C ++ class wrap (extension .pyx)
RNG.pyx
# distutils: language = c++
# distutils: sources = mt19937.cpp
cdef extern from "mt19937.h" namespace "mtrandom":
unsigned int N
cdef cppclass MT_RNG:
MT_RNG()
MT_RNG(unsigned long s)
MT_RNG(unsigned long init_key[], int key_length)
void init_genrand(unsigned long s)
unsigned long genrand_int32()
double genrand_real1()
double operator()()
cdef class RNG:
cdef MT_RNG *_thisptr
def __cinit__(self, unsigned long s):
self._thisptr = new MT_RNG(s)
if self._thisptr == NULL:
raise MemoryError()
def __dealloc__(self):
if self._thisptr != NULL:
del self._thisptr
cpdef unsigned long randint(self):
return self._thisptr.genrand_int32()
cpdef double rand(self):
return self._thisptr.genrand_real1()
Setup.py file that tells you how to compile
py.setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
ext = Extension("RNG",
sources=["RNG.pyx", "mt19937.cpp"],
language="c++")
setup(name="RNG",
ext_modules=cythonize(ext))
From here on, I'm trying to interpret Cython, but it may be wrong. Please refer to the same book.
Declaration of external C ++ code in Cython (extension .pxd)
How to write pxd file
cdef extern from "C++Header file in" namespace "C++Namespace in":
C++Constants defined by const static in the header file of
unsigned int N
cdef cppclass C++Class identifier in:
Constactor()
Constactor(unsigned long s)
Constactor(unsigned long init_key[], int key_length)
C used in Cython among public methods++Method declaration
void init_genrand(unsigned long s)
unsigned long genrand_int32()
double genrand_real1()
double operator()()
In the file downloaded from github, the declaration of external C ++ code in Cython is You have migrated to a pyx file.
Added description to pyx file to specify that it is C ++ language # distutils: language = c++
cdef cppclass MT_RNG:
In the block description of, the definition of the class and public method in the reference source C ++ is described.
How to write a pyx file
cdef class Class identifier in Cython:
cdef MT_RNG *_thisptr
def __cinit__(self, unsigned long s):
Constructor description
C to be wrapped in this++Create an instance of the class of.
The first self of the argument is common to the case of Python class.
self._thisptr = new MT_RNG(s)
if self._thisptr == NULL:
raise MemoryError()
def __dealloc__(self):
Special method to clean up
if self._thisptr != NULL:
del self._thisptr
Type method name in cpdef Cython(self,argument):
Implementation of methods in Cython grammar
In this, C declared by cdef extern++Functions and methods are available.
return Return value
cpdef unsigned long randint(self):
return self._thisptr.genrand_int32()
cpdef double rand(self):
return self._thisptr.genrand_real1()
setup.How to write py
from distutils.core import setup, Extension
from Cython.Build import cythonize
ext = Extension("The name of the python extension",
sources=["The name of the pyx file", "cpp file name"],
language="c++")
setup(name="RNG",
ext_modules=cythonize(ext))
Difference from C version: Added language = "c ++" to ext = Extension ().
Supplement: It is also possible to include C-level language element declarations in the pyx file instead of the pxd file. However, it is better to keep it as a separate pxd file for maintenance. as a trial, cdef extern from "mt19937.h" namespace "mtrandom": Even if you extract the block starting with from the pyx file to the pyd file and compile it Successful build and operation.
URL: Cython document (Japanese translation) tutorial http://omake.accense.com/static/doc-ja/cython/src/tutorial/index.html