On Friday, 30 April 2021 at 22:22:05 UTC, sfp wrote:


To be clear, I'm mostly interested in examples that show how to structure a project. I'd like to know what the best way to start using D in my current project is, how to call D from C, and how to call C from D.


There is nothing special about the structure of a BetterC _project_. It's just a D project without the full feature set, so you can structure it in whatever way makes sense to you in terms of packages and modules.

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.

Beyond that, it's just a matter of making use of a limited subset of D features:

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

I'm unaware of any specific open source projects that do what you're looking to (integrate new D code into an existing C project) especially any using CMake. But I would approach by isolating the new D code as a library. Then you can structure it however you like and just add the library to your C project's dependencies.

Alternatively, you could add individual D modules to your source tree and configure CMake such that they are compiled with a D compiler and a header generated for each.

I mean, there's no one way to go about this. But whatever you end up doing, please consider writing it up in a blog post for the D blog (email me at aldac...@gmail.com when you're ready) describing how you approached it, then your project could be an example for someone else.

Reply via email to