On Thursday, 26 May 2005 at 20:41:10 UTC, Vathix wrote:
The problem is that D's main() initializes things. Using a C
main() bypasses that startup code. Put the main() in the D file
(with D extern) and have it call a function in the C file that
you will treat as main.
That's correct, but not always an option, such as when writing a
D library which can be called from C programs you can't touch.
But you can easily do the initialization in your D code, by
calling rt_init() and rt_term(), like this:
import std.stdio;
import core.memory : GC;
extern(C) int rt_init();
extern(C) int rt_term();
extern(C) __gshared bool rt_initialized = false;
extern(C) void d_function(){
writeln("Initializing D runtime");
if(!rt_initialized)
rt_init();
rt_initialized = true;
char[] big = new char[10000000];
big = null;
writeln("Calling GC");
GC.collect();
writeln("Finishing D function");
scope(exit){
writeln("Terminating D runtime");
if(rt_initialized)
rt_term();
rt_initialized = false;
}
}
...just be careful that you don't do anything requiring memory
allocation before rt_init() or after rt_term().