What is the simplest way to create a pxd definition file, which simply collects cdefs replicated over pyx files, without creating new extensions?
My case is the following: I would like to gather some extern cdefs in a pxd file (hp/src/common.pxd). I’ve also added some non-extern cdef, whose implementation is in common.pyx.
In another pyx file (hp/src/_lib.pyx), which I turn into an extension, I cimport some stuff from common.
In the setup.py file I create the following extension:
Extension('hp._lib',
['hp/src/_lib.pyx'],
language='c++'),
By doing so, no common.cpp file is created, so it looks like dependencies are not automatically handled. That’s the first problem.
Then, manually running ‘cython –cplus common.pyx’ correctly creates a common.cpp file in the directory hp/src, and if I add ‘hp/src/common.cpp’ to the list of the extension sources, the command python setup.py installs everything without complaint, but then, importing the module hp triggers an ImportError: No module named common… from _lib.cpp…
I’m stuck here. Any idea?
You can include material from another file using the
includestatement.See the documentation here:
http://docs.cython.org/src/userguide/language_basics.html#the-include-statement
If you have external definitions in
common.pxdand implementations incommond.pyxyou will need to include them both. For example, in_lib.pyx:The contents of these files will then be compiled into your
_libmodule as if they were textually included at that point. i.e. acommon.cfile will not be generated, but rather included with_lib.c.The Cython compiler doesn’t recognise changes to includes when deciding if the
.cfile for the module needs to be regenerated, so remember to delete this before you recompile.