C Function Pointer Wrapping Example not working

Nick Craig-Wood nick at craig-wood.com
Sun Nov 16 17:29:58 CET 2008

Charlie <Charlie.Xia.FDU at gmail.com> wrote:
>  I am new to using swig/C++/python. I got some problem with function
>  pointers. I posted in swig-user, but got no response. So I forwarded
>  it here. You help is greatly appreciated.
>  Thanks!
>  Hi All,
>  Yesterday I posted about the question I had of template function. This
>  time I worked step step from examples in Doc 1.3 to reach my ultimate
>  goal. But as I worked on the Function Pointer Example in 5.4.9 in Doc
>  1.3, it doesn't work. My platform is a Ubuntu linux with swig 1.3.36,
>  python 2.5.2 and gcc 4.2.3.
>  The compiling and linking has no errors:
>  swig -python -c++ -o test_wrap.cpp test.i
>  python setup-test.py build
>  running build
>  running build_py
>  copying test.py -> build/lib.linux-i686-2.5
>  running build_ext
>  building '_test' extension
>  gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -
>  Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c test_wrap.cpp -o
>  build/temp.linux-i686-2.5/test_wrap.o
>  cc1plus: warning: command line option "-Wstrict-prototypes" is valid
>  for Ada/C/ObjC but not for C++
>  g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-
>  i686-2.5/test_wrap.o build/temp.linux-i686-2.5/test.o -o build/
>  lib.linux-i686-2.5/_test.so
>  But when I try to import test in python, it complains:
>  import _test
>  ImportError: ./_test.so undefined symbol: _Z9binary_opiiPFiiiE

The above is a mangled name so you've got some C vs C++ problems I'd

You could try putting some extern "C" {} in around all the functions
which are imported and exported.  Have a look at the code SWIG
generates and see if it puts some extern "C" in and match what it
does in your code.

We used to use SWIG in for python embedding in our C++ project, but we
found that using ctypes is a lot easier.  You just write C .so/.dll
and use ctypes to access them.  You can do callbacks and embedding
python like this too.

Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick

More information about the Python-list mailing list