Calling C functions inside Python
January 25, 2009
In my current work, I’m developing new ways to test a Telecom framework. Firstly, I was building automation tests with cgreen to blackbox/unit test model. But the development stages were going a little more slow than I thought because write and debug C code is not so easy.
Suddenly, I though…”Hey, why do I not build these tests with python scripts ???”. And my response was: “Ctypes is your friend!”
Ctypes permits you call C functions inside python code. It’s wonderful. Let’s see how to do it in OpenSolaris environment:
First, download ctypes:
$ wget -c \ http://ufpr.dl.sourceforge.net/sourceforge/ctypes/ctypes-1.0.2.tar.gz
Install ctypes in a local path (as normal user):
$ tar -zxvf ctypes-1.0.2.tar.gz
$ cd ctypes-1.0.2
$ python setup.py install --prefix=$HOME/python
Export your PYTHONPATH environment variable:
$ export PYTHONPATH=$HOME/python/lib/python2.4/site-packages
Testing ctypes:
$ python
>>> import ctypes
OK ! Ctypes is there, but how do I use it ???
See a little example that I made to explain how to use ctypes:
- A file named test.c with a function (sum) that returns a sum of two values:
int sum (int a, int b)
{
return (a+b);
}
- Compile it as a shared library:
$ gcc -fPIC -c test.c
$ gcc -shared -o test.o libtest.so
- Make sure that our library will be found
$ cp test.so $HOME/example
$ export LD_LIBRARY_PATH=$HOME/example
- Let’s call the function sum through a Python script
$ python
>>> from ctypes import *
>>> cdll.LoadLibrary('libtest.so')
<CDLL 'libtest.so', handle fe7806e0 at 80dfcec>
>>> libc = CDLL('libtest.so')
>>> libc.sum()
112710980
>>> libc.sum(1,2)
3
I can see a smile in your face. Yeah! Ctypes is a great trick to C programmers
Enjoy it!
Useful links:
- http://docs.python.org/library/ctypes.html









April 1, 2009 at 2:58 am
Thanks,
Great reference,
I picked some difficulties in testing it im my windows and Ubuntu.
Let me explain the problem and Solution to it too.
============**************==============
Error :
gcc -shared sum.o -o sum.so
/usr/bin/ld: crti.o: No such file: No such file or directory
collect2: ld returned 1 exit status
Error :
cdll.LoadLibrary(’sum.so’)
Traceback (most recent call last):
File “”, line 1, in
File “ctypes/__init__.py”, line 431, in LoadLibrary
File “ctypes/__init__.py”, line 348, in __init__
OSError: sum.so: cannot open shared object file: No such file or directory
=======***************==========
Solution to those problems
********==============*****************
For both OS
let’s say myfunction.c
int area_rect(int length, int breadth)
{
return length*breadth;
}
$ gcc -fPIC -c myfunction.c
$ gcc -shared myfunction.o -o myfunction.so
Note in Ubuntu i had error described in head, so i used
$ ld -shared myfunction.o -o myfunction.so
$ export LD_LIBRARY_PATH=$ # use it in ubuntu to describe the path
In windows, you can add the location in System variable path. Search in web, if you cannot.
Now use python
$python
>>>from ctypes import *
>>>cdll.LoadLibrary(‘myfunction.so’)
>>> libc = CDLL(‘myfunction.so’)
>>>libc.area_rect(12,34)
Got result ?
Good luck. You did it, enjoy
*************================***************