Using C++ and ctypes together: a vast conspiracy? ;)

Nick Craig-Wood nick at craig-wood.com
Wed Jun 3 00:29:43 CEST 2009


Diez B. Roggisch <deets at nospam.web.de> wrote:
>  Joseph Garvin schrieb:
> > So I was curious whether it's possible to use the ctypes module with
> > C++ and if so how difficult it is. I figure in principal it's possible
> > if ctypes knows about each compiler's name mangling scheme. So I
> > searched for "ctypes c++" on Google.
[snip]
> > More seriously -- how difficult is it to use ctypes instead of saying,
> > boost::python, and why isn't this in a FAQ somewhere? ;)
> 
>  Because it's much more needed than name-mangling. Name mangling is 
>  (amongst other things) one thing to prevent 
>  C++-inter-compiler-interoperability which results from differing C++ ABIs.
> 
>  I'm not an expert on this, but AFAIK no common ABI exists, especially 
>  not amongst VC++ & G++. Which means that to work for C++, ctypes would 
>  need to know about the internals of both compilers, potentially in 
>  several versions, and possibly without prior notice to changes.

Probably depends on how far you want to dig into C++.  I'm sure it
will work fine for simple function calls, passing classes as anonymous
pointers etc.  If you want to dig into virtual classes with multiple
bases or the STL then you are probably into the territory you
describe.

That said I've used C++ with ctypes loads of times, but I always wrap
the exported stuff in extern "C" { } blocks.

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



More information about the Python-list mailing list