Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use shared dynamic libraries with python-cffi (in linux)?

OS: CentOS 6 (64bit)

I have a dynamic library (.so) in C. And I want to create an abstraction layer of Python over it and then use it to implement my logic. I have decided to use CFFI for this since it doesn't deal with any kind of dsl (domain specific language).

Couple of things I wanted to know:

  1. Is there some good starting point which I can refer for doing this (loading and using dynamic libraries using cffi)? The docs on the official site talk about this, but I was looking if there was some concrete reference somewhere with some examples. Or someone who might have tried it.
  2. Can there a possible drawback to this approach?

Thanks

like image 555
utsavanand Avatar asked May 05 '26 17:05

utsavanand


1 Answers

Two good starting points:

  • The CFFI documentation, and specifically the ABI out of line example: https://cffi.readthedocs.org/en/latest/overview.html#out-of-line-example-abi-level-out-of-line

  • My CFFI example repository: https://github.com/wolever/python-cffi-example

Between the two you shouldn't have too much trouble putting together your wrapper.

And to your second question: if the shared library you're wrapping is very simple (ex, a few function calls, simle data structures) you might find ctypes simpler (as it's part of the standard library).

like image 78
David Wolever Avatar answered May 08 '26 11:05

David Wolever