Saturday, December 13, 2008

Swig and Python first experimentation

Today I tried swig to interface python to my C++ library.
According to this example it seems very simple but of course it doesn't work.
I have followed exactly the proposed steps and I got 3 problems:
  1. can't find Python.h: solution->sudo aptitude install python-dev
  2. ld -shared -o example.o example_wrap.old: example.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC : solution-> do it
  3. ld -bundle -flat_namespace -undefined suppress -o example.o example_wrap.o
    ld: -f may not be used without -shared :->remove -bundle and add -share option
Here is my config:
AMD Athlon X2 5400+ Dual-Core Processor
unbunto 8.4.10
Python 2.5.2

Fast track solution:
svn co swig-example
cd swig-example

Now here is exactly what you should do to make it working:
1) create example.c and example.i

2) create python interface to example module that contains cube function
sudo aptitude install swig
sudo aptitude install python-dev
swig -python example.i
gcc -c example.c example_wrap.c -I/usr/include/python2.5 -I/usr/lib/python2.5 -fPIC
ld -shared -o example.o example_wrap.o
ld -share -flat_namespace -undefined suppress -o example.o example_wrap.o

3) try python interface

fpieraut@fraka7:~/swig$ python
Python 2.5.2 (r252:60911, Oct 5 2008, 19:29:17)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> example.cube(3)

No comments:

Post a Comment