Fwd: Interfacing with C++

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 3 10:43:11 PST 2014


"Shriramana Sharma via Digitalmars-d"  wrote in message 
news:mailman.1464.1415039051.9932.digitalmars-d at puremagic.com...

> Hello. I really really need to be able to interface well with a C++
> library which contains lots of classes if I am going to further invest
> time into D.
>
> Now from the http://dlang.org/cpp_interface I find out the current
> status of built-in C++ interfacing support. I'm working on Linux so
> using the COM support is not an option for me (whatever COM may be!).

That page is out of date.  Support has improved significantly since then.

> A few queries:
>
> 1) How mature is the SWIG support for wrapping a library into D?
> Specifically does the above SWIG support take advantage of the
> built-in C++ support features like connecting directly to virtual
> functions?

No idea.

> 2) Is there any further work underway to implement support for static
> and non-virtual class member functions? Or is this not at all
> possible? If so, why?

Work is complete.  IIRC it was in the last release, you might need to grab 
master/an alpha for very latest stuff.  Special member functions 
(dtor/operators) generally don't work.

> 3) The page speaks about having to integrate an entire C++ compiler
> into the D compiler. Could the usage of libclang help here in having
> to avoid writing a new C++ compiler module or am I talking through my
> (non-existent) hat?

Yes.  But adding a second compiler to the compiler is problematic even if 
you don't have the write the second compiler.  You however could make a C++ 
binding generator with clang (see dstep).

Also, note that you can still use some aspects of C++ templates from D - you 
can pass a templated struct or class between the languages (but you will 
have to more or less re-write it on the other side).  This is done in ddmd 
(magicport2/newmagic) with root.array.

You can also call templated functions with some hacks, if you are careful 
about ensuring they're instantiated on the C++ side.  (ie use 
pragma(mangle))

So, a lot is possible, but when things go wrong you'll be fighting nasty abi 
and alignment bugs. 



More information about the Digitalmars-d mailing list