State of interfacing with c++

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Fri Mar 18 07:26:31 PDT 2016


On Friday, 18 March 2016 at 13:45:18 UTC, Jonas Drewsen wrote:
> For a hack week at work I am thinking about creating a module 
> in D that can be used with our existing application which is 
> written in C++.
>
> Can anyone shed some light on the current status of interfacing 
> with C++? In particular:
>
> 1, Can I build my C++ program using visual c++ and then link a 
> D module compiled with gdc/ldc/dmd into it using the ms linker?
>
> 2, Can I do the same with the OSX tool chain ie. compile main 
> program using xcode clang etc.
>
> 3, And what about linux?
>
> 3, Which features of D or C++ should I expect not to work or 
> shouldn't use when interfacing.
>
> Any pointers to reasonably up-to-date info about interfacing 
> with c++ is much appreciated as well.
>
> Thanks

If you need only static linking, 1, 2 and 3 should all work fine 
with DMD, if you call into the D module through extern(C) 
functions. If you want use C++ interface it's a little more 
complicated - see http://dlang.org/spec/cpp_interface.html, which 
was updated recently.
The important thing to remember is that the Druntime must be 
initialized (see http://wiki.dlang.org/Runtime_internals and 
http://dlang.org/phobos/core_runtime.html#.rt_init for more 
info), before you call D functions that use the garbage 
collector. If all your D code is @nogc nothrow, you shouldn't 
need to do any initialization.
AFAIK, GDC doesn't support Windows and is on par with DMD 2.067.
LDC recently got a lot of momentum - the LDC master branch is on 
par with DMD 2.069.2 which means it uses the DMD frontend which 
is written in D, while being a C++ application which uses the C++ 
LLVM interface. It's quite an accomplishment because it would 
make LDC 1.0.0 the largest mixed C++ and D application compiled 
with LDC. There's been also a lot of work on the MSVC front 
lately.


More information about the Digitalmars-d mailing list