# CFFI — card for AI agents

Foreign Function Interface for calling C code from Python.

Category: Languages, types and runtimes · PyPI package `cffi` · latest version 2.1.1 (checked 2026-11-01)
Install: `pip install cffi`
License: MIT-0 (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 64.9 (#678 of all tools, 17 of 70 in its category)
- Used in 39 public projects set up for AI coding agents (0.6%)
- Downloads last week: 298,090,885 (as of 2026-11-24), +16.6% over 4 weeks
- GitHub stars: 292 (as of 2026-11-24)
- Last push: 2026-11-18

## Often used with
pycparser 97.4%, Pydantic 92.3%, Click 87.2%, urllib3 87.2%, cryptography 84.6%

## Checked alternatives (same job)
- Cython: https://agentgid.com/tools/cython/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/cffi/
- Repository: https://github.com/python-cffi/cffi


## CFFI: use C libraries from Python code

CFFI is a free Python library that lets Python programs use code written in C. You copy a few C declarations, and Python can call a library already on the computer or a small fast function you wrote in C. It works without a C compiler, but with one it is safer and faster.

### Where to use it
- Use a C library already on the machine: Your Python app calls a C library that is installed on the computer, such as zlib, without writing any C and without a compiler.
- Speed up one slow calculation: A loop that is too slow in Python, like counting or scoring many items, moves into a small C function your Python code calls like any other.
- Wrap C source code you were given: You have the .c and .h files of a library, for example from a device maker, and turn them into one module your Python app imports.
- Read binary files with a fixed layout: Load pixel data or records from a binary file into C-shaped structures, change a few fields and write them back.
- Let a C program run Python code: Package Python code as a plug-in library that an existing C or C++ program loads and calls.
- Drive a device from a web backend: A Python backend controls a label printer or scanner through the maker's C library, so the web page can print or scan. (idea, not from the official pages)

### How to start
- Install it into your project's Python environment (Python 3.10 or newer).

```
pip install cffi
```

- Declare the C functions you need and load a library that is already installed (no compiler needed).

```
>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.cdef("""
...     int printf(const char *format, ...);   // copy-pasted from the man page
... """)
>>> C = ffi.dlopen(None)                     # loads the entire C namespace
>>> arg = ffi.new("char[]", b"world")        # equivalent to C code: char arg[] = "world";
>>> C.printf(b"hi there, %s.\n", arg)        # call printf
hi there, world.
```

- When a C compiler is available, prefer the compiled mode with set_source() and compile(); it is safer and faster.

### License
Free for any use, including paid and closed products, under the MIT No Attribution license. You do not even have to keep a credit line; a few files may carry their own license.

### Good to know
- It needs Python 3.10 or newer; the docs list Python 3.10 to 3.15 as tested.
- Without a compiler, a small mistake in the declarations makes the program crash instead of showing an error.
- It works with C only, not C++; for C++ code you add a small C layer or use another tool.
- The compiled mode needs a C compiler on the machine that builds the module, and setuptools on Python 3.12 and newer.
- Memory you create through it is freed as soon as Python no longer holds the object, even if C still points to it.

Checked 2026-11-11 against: https://github.com/python-cffi/cffi/blob/main/README.md, https://github.com/python-cffi/cffi/blob/main/LICENSE, https://pypi.org/pypi/cffi/json, https://cffi.readthedocs.io/en/stable/, https://cffi.readthedocs.io/en/stable/overview.html, https://cffi.readthedocs.io/en/stable/installation.html, https://cffi.readthedocs.io/en/stable/whatsnew.html, https://cffi.readthedocs.io/en/stable/using.html, https://cffi.readthedocs.io/en/stable/goals.html

### Mistakes to avoid
- Don't: pip install cffi only, then ffibuilder.compile() on Python 3.12+ → Do: Add setuptools to the project (pip install setuptools) next to cffi (In our run on Python 3.12 compile() stopped with "This CFFI feature requires setuptools on Python >= 3.12"; it worked after installing setuptools.)
- Don't: C.printf("hi %s", "world") with Python str arguments → Do: C.printf(b"hi %s", ffi.new("char[]", b"world")) or "text".encode("utf-8") (char * parameters take bytes, not str.)
- Don't: p = ffi.new("char **", ffi.new("char[]", b"hello")) → Do: s = ffi.new("char[]", b"hello"); p = ffi.new("char **", s)  # keep s in a variable while C uses it (The inner object has no Python reference, so its memory is freed at once and C reads garbage.)
- Don't: ffi.new("char *") to make a text buffer → Do: ffi.new("char[]", b"text") or ffi.new("char[]", size) for a buffer (char * allocates a single character, not a string.)
- Don't: ffi.dlopen(None) to reach the C standard library on Windows → Do: Open a specific DLL by name, e.g. ffi.dlopen("path.dll"), or use the compiled mode (On Python 3 under Windows dlopen(None) does not work.)

### Questions
- Do I need a C compiler? Not for the simple mode that loads an installed library. The compiled mode, which the docs recommend, needs one on the machine that builds the module.
- Why does compile() fail with "requires setuptools" on Python 3.12? Python 3.12 removed the old build tools CFFI used. Add setuptools to your project; CFFI does not pull it in by itself.
- Can I pass a normal Python string to a C function? No. Functions that take char * expect bytes, so encode the string first.
- Can I use it in a commercial product? Yes. The MIT No Attribution license allows any use, without a credit requirement.
- Is it still maintained? Yes. Version 2.1.1 came out in August 2026 and keeps up with new Python versions.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
