D and C++ currently interoperability status
Asked Answered
W

2

6

I'd like to learn D, but I haven't understood well an important thing. How's the current interoperability status between C++ and D?

I'm sure it's impossible to link to C++ compiled binaries, as it doesn't even work with C++ compilers. But what if I have the source code of a C++ library and some D source code. Would it be possible to make them speak? (hopefully in an efficient way).

How about the different C++ versions (C++98, 11, 14, 17 and future versions)?

Whisky answered 27/11, 2018 at 20:41 Comment(6)
The last time I did some C++ and D interop many years ago, I used a C ABI to bridge the two. The Interfacing to C++ documentation seems that there may be some (limited?) improvement.Pounds
Thwy can both interoperate with C. There's your (limited) bridge.Marti
What's the use case? C++ program that has some parts written in D? D program that needs to use some code written in C++? Experiment for fun? Incremental porting of a program from C++ to D (or vice versa)? If the use case is "I'd like to learn D", I'd say: just learn D, don't add C++ in the mix it will just make your experience learning D miserable.Pounds
Yep, a C ABI it's ok for binaries too. @Pounds at the moment it's just an experiment, but if the experiment works I'd probably like to use existing C++ libraries, especially some ones that use templatesWhisky
Actually, D can interop with C++ binaries to some extent. It does this by copying the conventions of the most popular compiler on the given platform. So it isn't a standard per se, but it does work in a lot of cases. You can access some C++ classes directly. But the details is in the "some" - in particular, templates only work if they are already instantiated in the C++ binary library (D can NOT read C++ source code), and C++ classes might need a factory function written in c++. The documentation is kinda poor tho cuz this changes a lot...Iosep
thank you very much. I understand and I think it's quite normal it changes a lot. I hope the future will be very good for Dlang as it seems a wonderful languageWhisky
E
8

D interoperability with C++ has been improved considerably in the last few years. The "Interfacing to C++" section of the "D Specification" is a good start if you want to learn more.You may also want to look at the "magical" dpp project - https://code.dlang.org/packages/dpp .

I do not understand your question about linking... The linker is the same no matter whether you use Assembly, C, C++, D, or any combination of them... You will be able to link C++ libraries, but you may not be able to use all what is inside (depends on what is in the library). I've seen D apps linked against Boost libraries for an example.

Electrotherapy answered 27/11, 2018 at 21:9 Comment(1)
it seems indeed magical.. even if "It currently only supports C features, but C++ is planned." hopefully it will be soonWhisky
I
8

I'm part of the compiler team and have been working on the C++ interop this summer, to interact with a piece of C++ code written in modern C++14.

C++ is very well supported by D, in fact it probably has one of the best support for it out there, for a language of this size. You can, for example, throw any exception derived from std::exception in C++ and catch it on the D side. You can write a class in D and use it from D (virtual method or not), or the other way around. You can subclass a D class from C++, and you can subclass a C++ class from D. You can call instantiated templated functions!

Now, there are a few catches:

  • If you are doing something not very idiomatic in C++, e.g. throwing int, that's going to be a problem.
  • The C++ code should not break D's type system. E.g. you cannot represent a char* const* (pointer to constant pointer to mutable char) because D's const is transitive, so it has to be const char* const*.
  • The cross-version support is still not optimal. There is currently a mix of C++98 and C++11, and no way to control what to use. It does not matter in 95% of the cases, since D interfaces at the binary level, and is only concerned about calling convention and symbol mangling.

As for platforms, Windows is very well supported (we have some game devs using it). POSIX support for calling templated functions is more recent, but works for me.

That's it for the language support. The tools around it (e.g. what Dejan mentioned) are still being built, and there's a big interest from the industry in having them, as D is regarded as much more pleasant to use, and it can offer an easy transition path for C++ developers. In fact, all 3 D compilers (DMD, GDC, LDC) use a common frontend written in D, and exposed to C++ their backend (DMC, GCC, LLVM, respectively).

TL;DR:

  • If you want to call C++ and can spare the time to write some bindings in D for your classes / functions, yes.
  • If you cannot write the bindings, stay tuned.
  • If you want to expose D code to C++, a resounding yes!
Indebted answered 10/12, 2018 at 16:58 Comment(1)
thank you very much for the explanation, it's really clear nowWhisky

© 2022 - 2024 — McMap. All rights reserved.