Playground for learning and testing cffi python interface. In order to use cffi, install it! See this page http://cffi.readthedocs.org/ for more info.
###Example 1### Shows how to build C shared library and use it via cffi.
In order to run test1 change to directory test1.
cd test1 && make
Then you can execute the cffi_test.py
from any directory e.g. from the root of this repo.
Do not forget to set the LD_LIBRARY_PATH
variable to directory test1
in this repo. There is located the just compiled shared library.
LD_LIBRARY_PATH=test1 python test1/cffi_test1.py
###Example 2### Shows how to wrap simple C++ shared library with C compatible header and use it in cffi.
In order to run test2 change to directory test2.
cd test2 && make
From that directory you can run
- The
C
program which wrapsC++
library - The Python program which uses the
C++
library withC
linkage
# C program launched from test2 directory
test2$ LD_LIBRARY_PATH=. ./prog my test parameters get printed
I am C program
It works!:
./prog
my
test
parameters
get
printed
# Python program launched from test2 directory
test2$ LD_LIBRARY_PATH=. python cffi_test2.py test parameters get printed
It works!:
cffi_test2.py
test
parameters
get
printed
I tried to use shared library which needs another shared library.
In our example libtest1.so
needs libtest2.so
.
I am using ffi.verify
function.
To compile the library and test the example run following commands.
cd test3 && make # Compile shared library and testing C program
./prog # Run the testing C program
./cffi_test3.py # Run the example.
- http://eli.thegreenplace.net/2013/03/09/python-ffi-with-ctypes-and-cffi/
- https://groups.google.com/forum/#!msg/python-cffi/
- https://pypi.python.org/pypi/cffi
- http://cffi.readthedocs.org/
- https://github.com/search?q-cffi+python&type-Repositories&ref-searchresults
- https://github.com/felipecruz/zmqpy # failing imports
- https://github.com/apendleton/python-lz4-cffi # todo
- https://github.com/SimonSapin/cairocffi # looks maintened enough
- Always compile with gcc
- Name your files
*.c
not*.cc
otherwisegcc
will compile it like C++ file - During linking your shared library put
-Lpathtoyourlibdirectory -lyourlibname
as last parameters togcc
.