betterC examples?

Mike Parker aldacron at gmail.com
Sat May 1 01:52:00 UTC 2021


On Saturday, 1 May 2021 at 01:29:05 UTC, Mike Parker wrote:

>
> As for the code, your main function should be `extern(C)`. 
> Functions you want to make available to the C side should also 
> be `extern(C)`, and will need equivalent declarations in C. DMD 
> has an experimental feature to generate C headers for you via 
> the `-HC` (`dmd -HC=?` for options). Then all that's left is to 
> link the C objects into D or the D objects into C.
>

Forgot to talk about going the other way. You'll need to declare 
in D any C functions you'd like to call, also as `extern(C)`, 
with the appropriate type translations. There are posts about 
that on the D Blog:

https://dlang.org/blog/the-d-and-c-series/

Some documentation here:

https://dlang.org/spec/interfaceToC.html

A page on the D Wiki:

https://wiki.dlang.org/D_binding_for_C

And a rather lengthy chapter in my book 'Learning D'. (Email me 
about that chapter if you'd like).






More information about the Digitalmars-d-learn mailing list