Dcalled From C
![]() |
|
Calling D functions from C
This is possible although not emphasised with a heading - see the Interfacing page ( D 2.x,
D 1.x) in the D language description. All that is needed is to declare the function as
![]() |
|
Example D function
![]() |
|
Problem and Solution
The problem is that a C program which calls only this, does not link as there are undefined references _deh_beg and _deh_end . To resolve these it is necessary to define and call a D main program. This can be done in a routine callable from C.
![]() |
|
Working C program
![]() |
|
Output
![]() |
|
Puzzle and /Discussion
The above also works with only the definition of the D main() routine and without the call. I am not clear as to whether that is just that this example is so simple. Does the D main() do some housekeeping which is needed? Should there be call to something else at exit, or has it been done when main() exited?
- There is indeed some housekeeping going on in dmd/src/phobos/internal/dmain2.d
- For some sample code see
NG:digitalmars.com.D.gnu/1436
- ThomasKühne
Another Example (under linux)
Dmaindummy.d
![]() |
|
Dfunc.d
![]() |
|
Cmain.c
![]() |
|
Put it all together!
![]() |
|