Calling C code from Python via Cython

If you need more information what Cython is, take a look here:

This sample, is a simple “Hello world” code for macOS based installation of Python and Cython.

First step is to get Cython for your platform. You can use pip to achieve that

# Installation of Cython at macOS
# I prefer to use Virtualenv contrary to OS based installation
# If you need more info about Virtualenv, take a look here

> virtualenv venv
> source venv/bin/activate
> pip install Cython

After you are done with that, you can create sample code. We will call method in C that accepts one argument – char * – and prints the string on stdout.

/* hello.c */
#include <stdio.h>

void f(char *str) {
      printf("str: %s\n", str);

Next thing we need is a Cython based wrapper for the code we want to call

''' hello_caller.pyx '''

cdef extern from "hello.c":
  void f(char *)

    take a look here for details about different Cython declarations
cpdef hello_world(str):

Last thing we need is a script for Cython that will perform the build

''' '''
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

sourcefiles = ['hello_caller.pyx']
ext_modules = [Extension("hello_caller",
                          sourcefiles )]

  name = 'Hello world',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules

After everything is set up, we can build everything

> python build_ext --inplace

and, eventually, we can call C code from Python code

''' '''

import hello_caller

message = "Hello world"

by running

> python ./
str: Hello world