Using ImportC to augment a big C project with D

Carl Sturtivant sturtivant at gmail.com
Tue Feb 20 18:33:42 UTC 2024


I just saw the announcement that macros with parameters are now 
translated into D by ImportC. Incredible! Congratulations to all 
involved.

As an occasional D user, I have long wanted a fast route to using 
D with an existing large C project (100K lines approximately).

I want to replace the C main program with a D main function that 
calls the old C main function (suitably renamed) to change the 
command line arguments that can be supplied, and with the helpful 
side effect that druntime is properly initialized. I want to 
replace some C files with D ones. (I am cognizant of GC issues 
here, this is not what I am asking about.)

Now I can use ImportC with all of the header files to make the D 
replacement files easy to write correctly. Perhaps I can use 
ImportC for all of the C source.

1.
When the resulting executable runs it will have D call C which in 
turn calls D. In that last D (the D files replacing some C 
files), if I throw an exception and don't catch it in the D files 
that replace some C files, will it propagate correctly to the D 
main function where I can catch it?

2.
The C source calls exit() from C's stdlib, and D needs to 
terminate properly. Throwing an exception and catching it in D's 
main function seems to be a way to achieve this. What is the best 
way to deal with exit() ?

3.
I want to use D's remarkable Fiber class to reimplement a 
deterministic stack changing context switch that in the original 
project is achieved with assembly code on a per-platform basis. 
This will mean that D's main function will call C which will call 
D which will effect a context switch. Is there any reason to 
believe this will work as naively hoped?




More information about the Digitalmars-d-learn mailing list