Thread overview
betterC examples?
Apr 30
sfp
Apr 30
bachmeier
May 01
sfp
6 days ago
sfp
April 30

I'm developing a C library with Cython bindings, built using CMake. I'd like to use D with -betterC to write some new code where it would be handy to have access to some more advanced language features to keep things readable. For my domain, C is totally fine 99% of the time, and rewriting a bunch of C code that's already tested and works nicely isn't a priority for me. I would like to develop new "modules" using -betterC and freely mix them into my existing C code without it being a headache.

So, it would be helpful to see some real -betterC examples. They don't need to polished, but substantial enough to help me get my bearings. Are there any out there that use CMake? This seems like it should be natural: start with a C or C++ CMake project (common enough!), and work D into the mix. I'm aware of cmake-d, but haven't taken too close of a look at it. It doesn't look like it's being actively developed.

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.

Thanks!

April 30

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

>

I'm developing a C library with Cython bindings, built using CMake. I'd like to use D with -betterC to write some new code where it would be handy to have access to some more advanced language features to keep things readable. For my domain, C is totally fine 99% of the time, and rewriting a bunch of C code that's already tested and works nicely isn't a priority for me. I would like to develop new "modules" using -betterC and freely mix them into my existing C code without it being a headache.

[...]

There is a blog series https://dlang.org/blog/the-d-and-c-series/#betterC

May 01

On Friday, 30 April 2021 at 23:06:48 UTC, bachmeier wrote:

>

There is a blog series https://dlang.org/blog/the-d-and-c-series/#betterC

Thanks for the link.

I read through these, but they don't address my question. The first two posts are about specific -betterC features and the third one is about a total conversion from C to -betterC of a one or two file program. It's very cool that you can do the conversion so is easily, but this isn't my goal.

May 01

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

May 01

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 aldacron@gmail.com when you're ready) describing how you approached it, then your project could be an example for someone else.

May 01

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).

6 days ago

On Saturday, 1 May 2021 at 01:52:00 UTC, Mike Parker wrote:

>

...

Mike, thanks for your detailed replies. They're both very helpful.

What you described is what I figured I would need to do if I were to roll my own solution. I was hoping someone would have gone through the trouble of doing this already, but it seems like maybe no one has.

Since this will require a bit more up front effort, I won't have a chance to give this as a go until the middle of summer or so. But I would be happy to put together a simple example as a GitHub repository and write it up for the D blog when I do so in case it's helpful to other people.

I'll also send you an email about that chapter (I'm assuming it's not publicly available).

If anyone else does know of some real-world examples, or has tried this kind of thing and has instructive war stories, please loop me in.