Compililng C++ and D together without going mad
Johan
j at j.nl
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).
>> https://github.com/ldc-developers/ldc
>>
>> -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:
> https://github.com/jupyter-xeus/xeus/blob/master/include/xeus/xkernel.hpp
> https://github.com/jupyter-xeus/xeus/blob/master/include/xeus/xkernel_configuration.hpp
> 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:
https://dlang.org/phobos/core_runtime.html#.rt_init
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
the_real_main(....);`
- 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.
-Johan
More information about the Digitalmars-d-learn
mailing list