Hi,
On 14 March 2018 at 22:11, <
san...@nyu.edu> wrote:
> And these two functions will be exposed into the lib object in Python.
> Currently, my setup without the class works and I really need this class to
> be a global variable so that I can reuse the C/C++ code to initialize and
> operate on this class.
>
> Is this the right way to go? Could somebody suggest other ways possible to
> wrap C++ code in C so that I can somehow proxy and expose via CFFI?
Yes, that's the way to do it: by exposing a C API from set_source().
Something like:
ffibuilder.set_source("mymodule", r'''
extern "C" {
int somefunc(int somearg) { return real_cpp_func(somearg); }
}
''', source_extension='.cpp')
The ``source_extension`` keyword makes sure the C compiler treats it
as C++. The ``extern "C"`` bit in the C++ code makes sure that the
function name is not mangled in the compiled module.
If storing things as globals works fine in your use case, then that's
it, but if you need really to be able to pass around pointers to C++
instances to Python, you can cast them to ``void *``. Or, write
``struct my_class_in_c;`` in the ``.h`` part, i.e. declare a
completely opaque struct type, and cast back and forth to ``struct
my_class_in_c *``.
A bientôt,
Armin.