Hi Matthias,
On 25 February 2017 at 13:45, Matthias Geier <
matthia...@gmail.com> wrote:
> I would like to create a re-usable CFFI module, which slightly
> simplified could look something like this:
It's not directly supported. If you write one_function() in the first
module, then you get a compiled library named something like "_one.so"
(python 2.7) or "_
one.cpython-35.so" (python 3.5); similarly for the
second module. The problem is that you want to call code from
"_one.*so" from inside "_two.*so". In order to do so, you need to
know exactly how "_one.*so" is called and pass the correct option to
the linker: for example, ``libraries=['_one']`` or (untested)
``libraries=['_one.cpython-35']``. You probably have to convince gcc
to look in the current directory: ``library_dirs=['.']``. Finally,
you might have to add more horrible tweaks for "_two.*so" to locate
"_one.*so" at runtime, though that's not needed on all operating
systems if you import "_one" before "_two". (The tweaks are very
OS-dependent and noted in the page
http://cffi.readthedocs.io/en/latest/embedding.html#issues-about-using-the-so.)
A better approach is probably to move the logic you want to reuse
inside some general C library, which you compile the normal way for a
C library; then use that from both "_one" and "_two".
Of course, if the two parts of the code are closely tied together,
then it's much simpler to just generate a single CFFI module. You
shouldn't think "it's extra code that is not always necessary", as
that argument doesn't really apply on modern OSes which will load only
the parts of a library that are actually used anyway.
Also, note that cffi has support for ffi.include(), which builds a FFI
object that extends another. It mostly means sharing the base's types
and constants. That's essentially a way to make an interface over two
existing C libraries, one of which depends on the other. In C, the
two libraries would come with C headers "first.h" and "second.h", and
the file "second.h" typically starts with ``#include "first.h"`` and
then adds its own functions. But your question seems to be about
reusing code compiled *inside the CFFI first interface,* which is
different.
A bientôt,
Armin.