This example shows how to quickly generate bindings for a simple C library using ctypesgen and access them in a Python script.
-
Compile the shared C library
gcc -fPIC -shared demolib.c -o demolib.so
-
(Re-)Generate the bindings (or you can just try the pre-generated bindings already present in this directory)
ctypesgen -i demolib.h -l demolib -L . -o pydemolib.py
-
Run the app that uses these generated bindings
python demoapp.py
The call should yield the following output:
a 1 b 2 result 3
-
You can also try calling the same code from a C executable
-
Compile test code:
gcc demoapp.c demolib.c demolib.h -o demoapp
-
Run
./demoapp
-
Observe the same results as before:
a 1 b 2 result 3
-
This demo was originally written by Chris Clark (clach04), when ctypesgen was still residing on code.google.com
.