Compililng C++ and D together without going mad

Johan j at
Wed Apr 29 13:12:50 UTC 2020

On Wednesday, 29 April 2020 at 13:02:36 UTC, Jan Hönig wrote:
> On Wednesday, 29 April 2020 at 11:38:16 UTC, Johan wrote:
>> LDC is a (somewhat complex) project with D and C++ code (and 
>> external C++ libraries).
>> I think it will help you if your main() is in D (such that 
>> druntime is automatically initialized for you).
>> -Johan
> Hmmm, a D main means i need to do quite some C++ bindings, with 
> which I have 0 experience. I cannot even say if it will be 
> possible.
> I will probably need:
> c++ stdlib's unique_ptr.
> And then i need to subclass a C++ class and extern this D 
> subclass.
> It sounds easier to me, to manually initialize D's runtime (i 
> imagine it being some function calls), build everything with 
> C++, and just call D functions from C++.

Manually initializing D's runtime is also possible. You need to 
call rt_init and rt_term:

What I meant is to
- add a `int main(....)` in a D source file
- rename your current `main` in the C++ source file to something 
like `the_real_main`
- add a forward reference to the D file: `extern(C) int 
- call `the_real_main` from the `main` in the D file

That way you don't have to think about how to correctly 
initialize D's runtime.


More information about the Digitalmars-d-learn mailing list