Skip to content

Commit fc092a1

Browse files
committed
Added CFFI section
1 parent 7d0139a commit fc092a1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docs/scenarios/clibs.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
Interfacing with C/C++ Libraries
22
================================
33

4+
C Foreign Function Interface
5+
----------------------------
6+
7+
`CFFI <https://cffi.readthedocs.org/en/latest/>`_ provides a simple to use
8+
mechanism for interfacing with C from both CPython and PyPy. It supports two
9+
modes: an inline ABI compatibility mode (example provided below), which allows
10+
you to dynamically load and run functions from executable modules (essentially
11+
exposing the same functionality as LoadLibrary or dlopen), and an API mode,
12+
which allows you to build C extension modules.
13+
14+
ABI Interaction
15+
~~~~~~~~~~~~~~~
16+
17+
.. code-block:: python
18+
:linenos:
19+
20+
from cffi import FFI
21+
ffi = FFI()
22+
ffi.cdef("size_t strlen(const char*);")
23+
clib = ffi.dlopen(None)
24+
length = clib.strlen("String to be evaluated.")
25+
# prints: 23
26+
print("{}".format(length))
427
528
ctypes
629
------

0 commit comments

Comments
 (0)