Compiling Cython extensions
The Cython syntax is, by design, a superset of Python. Cython can compile, with a few exceptions, most Python modules without requiring any change. Cython source files have the .pyx
extension and can be compiled to produce a C file using the cython
command.
Our first Cython script will contain a simple function that prints Hello, World!
 as the output.
Create a new hello.pyx
file containing the following code:
def hello(): print('Hello, World!')
The cython
command will read hello.pyx
and generate the hello.c
file:
$ cython hello.pyx
To compile hello.c
to a Python extension module, we will use the GCCÂ compiler. We need to add some Python-specific compilation options that depend on the operating system. It's important to specify the directory that contains the header files; in the following example, the directory is /usr/include/python3.5/
:
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -lm -I/usr/include/python3.5/ -o hello.so hello.c