Using C++ and ctypes together: a vast conspiracy? ;)
Sebastian Wiesner
basti.wiesner at gmx.net
Tue Jun 2 19:11:39 EDT 2009
<Nick Craig-Wood – Mittwoch, 3. Juni 2009 00:29>
> 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.
Name mangeling starts with namespaces, and namespaces are a thing often seen
in well-designed C++-libraries. So even a simple C++-function call could
become rather difficult to do using ctypes ...
> That said I've used C++ with ctypes loads of times, but I always wrap
> the exported stuff in extern "C" { } blocks.
No wonder, you have never actually used C++ with C types. An extern "C"
clause tells the compiler to generate C functions (more precisely, functions
that conform to the C ABI conventions), so effectively you're calling into
C, not into C++.
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
More information about the Python-list
mailing list