On Tuesday, 8 December 2020 at 01:47:51 UTC, Andrew Edwards wrote:

Thanks Jacob. The extern(C) is temporary. I'm doing a direct port of the code to D using -betterC. Wanted to keep it as close to the original as possible until everything compiles.

Yes, that's always a good idea. Do the initial port as close as possible to the original code. Then refactor and start adding D specific features.

I noticed that static this() does not work if either it or main() (or both) is marked extern(C).

Yes, that's expected. If you have a regular D main function, the compiler will recognize that and output an undefined symbol to the C main function. The C main function is implemented in the D runtime. The C runtime will call the C main function in the D runtime. The C main function is responsible for initializing the D runtime. This includes (not a complete list):

* Converting the arguments passed to the C main function to `string[]`
* Initializing the garbage collector
* Calling `shared static this()` in all modules
* Calling `static this()` in all modules
* Running the any unit tests
* Finally calling the D main function

When you define a C main function the compiler will recognize that and not output an undefined symbol. That means the C main function in the D runtime will never be called. That means the D runtime is never initialized. This is to allow you to use D without its runtime or get more control of the startup phase.

So either either you define a D main function and everything works as expected or you define a C main function and then you need to manually initialize and deinitialize the D runtime, if you want to use it. This can be done with [1] and [2].

[1] https://dlang.org/phobos/core_runtime.html#.rt_init
[2] https://dlang.org/phobos/core_runtime.html#.rt_term

--
/Jacob Carlborg

Reply via email to