Thursday, December 23, 2010

Cython 3: my own .c source file

Yet more on elementary Cython. I have a working example with my own C code, thanks to help from the Cython users' list (here)---see the UPDATE below for one small change:

hello2.c:

#include <stdio.h>
void f();

void f() {
printf("%s", "Hello world!\n");
}

hello2_caller.pyx:

cdef extern from "hello2.c":
void f()

cpdef myf():
f()

setup.hello2_caller.py:

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

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

setup(
name = 'Hello2 app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)

build it:

python setup.hello2_caller.py build_ext --inplace

import and use it:

>>> import hello2_caller
>>> hello2_caller.myf()
Hello world!

The compiler complains:

In file included from hello2_caller.c:219:
hello2.c:3: warning: function declaration isn’t a prototype
hello2.c:5: warning: function declaration isn’t a prototype

But it works.

UPDATE: the problem is that these two declarations are not the same:

void f();
void f(void);

and we need to have the second one.


$ python setup.hello2_caller.py build_ext --inplace
running build_ext
cythoning hello2_caller.pyx to hello2_caller.c
building 'hello2_caller' extension
creating build
creating build/temp.macosx-10.6-universal-2.6
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c hello2_caller.c -o build/temp.macosx-10.6-universal-2.6/hello2_caller.o
gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -arch i386 -arch ppc -arch x86_64 build/temp.macosx-10.6-universal-2.6/hello2_caller.o -o hello2_caller.so



$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello2_caller
>>> hello2_caller.myf()
Hello world!