Jump to page: 1 2
Thread overview
Best way to use large C library in D as of 2024
Mar 26
bachmeier
Mar 26
bachmeier
Mar 30
harakim
March 26

Hi D

I have a C library I use for work, it's maintained by an external organization that puts it through a very through test framework. Though source code is supplied, the intended use is to include the header files and link against pre-compiled code.

What is the best way, as of 2024 to use this library with D, in particular dmd? ImportC doesn't seem like the go-to option since the C source does not need to be complied. I've seen some forum post indicating that manually generated D wrappers are no longer needed, but I'm fuzzy on the particulars. If there's any blog posts or other instructions you'd like to reference I'd be happy to check them out.

For reference here's the C library in question: https://naif.jpl.nasa.gov/naif/toolkit_C_PC_Linux_GCC_64bit.html

Thanks for any guidance you can provide,

March 26

On Tuesday, 26 March 2024 at 19:24:39 UTC, Chris Piker wrote:

>

Hi D

I have a C library I use for work, it's maintained by an external organization that puts it through a very through test framework. Though source code is supplied, the intended use is to include the header files and link against pre-compiled code.

What is the best way, as of 2024 to use this library with D, in particular dmd? ImportC doesn't seem like the go-to option since the C source does not need to be complied. I've seen some forum post indicating that manually generated D wrappers are no longer needed, but I'm fuzzy on the particulars. If there's any blog posts or other instructions you'd like to reference I'd be happy to check them out.

For reference here's the C library in question: https://naif.jpl.nasa.gov/naif/toolkit_C_PC_Linux_GCC_64bit.html

Thanks for any guidance you can provide,

Should be able to just use it, as described here: https://forum.dlang.org/post/qxctappnigkwvaqakeqf@forum.dlang.org Create a .c file that includes the header files and then call the functions you need.

March 26

On Tuesday, 26 March 2024 at 20:19:27 UTC, bachmeier wrote:

>

Should be able to just use it, as described here: https://forum.dlang.org/post/qxctappnigkwvaqakeqf@forum.dlang.org Create a .c file that includes the header files and then call the functions you need.

Wow. That just worked the first time! Holy &^@$ that's easy!

So why does the 2nd page returned from the google search

interfacing with C site:dlang.org

(which happens to be: https://dlang.org/spec/interfaceToC.html) still have this text:

>

Since D can call C code directly, it can also call any C library functions,
giving D access to the smorgasbord of existing C libraries. To do so, however,
one needs to write a D interface (.di) file, which is a translation of the C .h
header file for the C library into D.

For popular C libraries, the first place to look for the corresponding D interface
file is the Deimos Project. If it isn't there already, please write and contribute
one to the Deimos Project.
?

This lead me to believe that interfacing was a chore and I was considering going back to C for a small program I need.

March 26

On Tuesday, 26 March 2024 at 20:42:00 UTC, Chris Piker wrote:

>

On Tuesday, 26 March 2024 at 20:19:27 UTC, bachmeier wrote:

>

Should be able to just use it, as described here: https://forum.dlang.org/post/qxctappnigkwvaqakeqf@forum.dlang.org Create a .c file that includes the header files and then call the functions you need.

Wow. That just worked the first time! Holy &^@$ that's easy!

So why does the 2nd page returned from the google search

interfacing with C site:dlang.org

(which happens to be: https://dlang.org/spec/interfaceToC.html) still have this text:

>

Since D can call C code directly, it can also call any C library functions,
giving D access to the smorgasbord of existing C libraries. To do so, however,
one needs to write a D interface (.di) file, which is a translation of the C .h
header file for the C library into D.

For popular C libraries, the first place to look for the corresponding D interface
file is the Deimos Project. If it isn't there already, please write and contribute
one to the Deimos Project.
?

This lead me to believe that interfacing was a chore and I was considering going back to C for a small program I need.

It's a recent thing that it works this well. Hopefully that page gets updated.

March 30

On Tuesday, 26 March 2024 at 20:42:00 UTC, Chris Piker wrote:

>

On Tuesday, 26 March 2024 at 20:19:27 UTC, bachmeier wrote:

>

Should be able to just use it, as described here: https://forum.dlang.org/post/qxctappnigkwvaqakeqf@forum.dlang.org Create a .c file that includes the header files and then call the functions you need.

Wow. That just worked the first time! Holy &^@$ that's easy!

So why does the 2nd page returned from the google search

interfacing with C site:dlang.org

(which happens to be: https://dlang.org/spec/interfaceToC.html) still have this text:

>

Since D can call C code directly, it can also call any C library functions,
giving D access to the smorgasbord of existing C libraries. To do so, however,
one needs to write a D interface (.di) file, which is a translation of the C .h
header file for the C library into D.

For popular C libraries, the first place to look for the corresponding D interface
file is the Deimos Project. If it isn't there already, please write and contribute
one to the Deimos Project.
?

This lead me to believe that interfacing was a chore and I was considering going back to C for a small program I need.

@D Language Foundation - This is a HUGE selling point. I had to use cups the other day and I just copied some code from a d file and linked the library. It was so easy I was suspicious but it worked. Using C from D is pretty much as easy as using C from C and I think you should advertise that better!

March 30

On Saturday, 30 March 2024 at 05:01:32 UTC, harakim wrote:

>

@D Language Foundation - This is a HUGE selling point. I had to use cups the other day and I just copied some code from a d file and linked the library. It was so easy I was suspicious but it worked. Using C from D is pretty much as easy as using C from C and I think you should advertise that better!

Though I appreciate the sentiment, it's much more effective and efficient for people actually using the feature, and who appreciate it, to write up a blog post about it somewhere and share that on Twitter/Reddit/HN, etc.

Just a handful of people doing that would potentially reach a wider audience, they'd be able to show the feature in use in actual code, and their words will probably carry more weight as a language user than someone from the DLF. There's a world of difference between showing it in use in a real-world project vs. showing examples of how it can be used, which is all that I'd be able to do if I were to write about it.

And that goes for anything that makes you think "the DLF should advertise this". I mean, when I had time to prioritize the blog, I was regularly writing articles about cool D features, and sometimes getting others to write guest posts about their projects or whatever. But that's pretty much like one person standing on a hilltop with a megaphone, periodically handing it off to someone else.

How many other people from the D community were adding their voices? Now and then, someone would write something up somewhere, but it wasn't happening regularly or often.

Anyone happy using D, who wants to see it continue to grow and improve, can help toward that end by telling the world about it.

That said, if there's anyone out there who would like to take on management of the blog and make it more active, I'd love to hear from you.

April 01

On Saturday, 30 March 2024 at 05:01:32 UTC, harakim wrote:

>

On Tuesday, 26 March 2024 at 20:42:00 UTC, Chris Piker wrote:

>

On Tuesday, 26 March 2024 at 20:19:27 UTC, bachmeier wrote:

>

Should be able to just use it, as described here: https://forum.dlang.org/post/qxctappnigkwvaqakeqf@forum.dlang.org Create a .c file that includes the header files and then call the functions you need.

Wow. That just worked the first time! Holy &^@$ that's easy!

So why does the 2nd page returned from the google search

interfacing with C site:dlang.org

(which happens to be: https://dlang.org/spec/interfaceToC.html) still have this text:

>

Since D can call C code directly, it can also call any C library functions,
giving D access to the smorgasbord of existing C libraries. To do so, however,
one needs to write a D interface (.di) file, which is a translation of the C .h
header file for the C library into D.

For popular C libraries, the first place to look for the corresponding D interface
file is the Deimos Project. If it isn't there already, please write and contribute
one to the Deimos Project.
?

This lead me to believe that interfacing was a chore and I was considering going back to C for a small program I need.

@D Language Foundation - This is a HUGE selling point. I had to use cups the other day and I just copied some code from a d file and linked the library. It was so easy I was suspicious but it worked. Using C from D is pretty much as easy as using C from C and I think you should advertise that better!

It works well if you only need to work with a header. There are still a few rough edges that get in the way if you're compiling the full C sources (I filed bugs for all of them):

  • Can't handle va_arg
  • Can't cast to a pointer of a struct that's typedef'd
  • Can't use complex numbers with the ternary operator

These problems should be cleaned up before heavily promoting what is an incredible feature. I don't think it's widely known that you can translate C source files into D. I think that's really cool, but in addition to the bugs listed above that ImportC can't handle, it outputs things like dpulicate aliases, function argument names that are D keywords, and declaring unions inside structs equal to void. All of these are easy to fix by hand, but it's time consuming.

Once these final odds and ends are working, we have a killer language feature.

April 12

On Saturday, 30 March 2024 at 07:11:49 UTC, Mike Parker wrote:

>

Though I appreciate the sentiment, it's much more effective and efficient for people actually using the feature, and who appreciate it, to write up a blog post about it somewhere and share that on Twitter/Reddit/HN, etc.

I would, but I'm just not a social media person. I pretty much only post online content here and at github.com. You may have the problem that D doesn't attract "very-online" personality types. I do mention D in work presentations, but those are not visible to the public.

April 12

On Monday, 1 April 2024 at 02:08:20 UTC, Lance Bachmeier wrote:

>

On Saturday, 30 March 2024 at 05:01:32 UTC, harakim wrote:
It works well if you only need to work with a header. There are still a few rough edges that get in the way if you're compiling the full C sources (I filed bugs for all of them):

  • Can't handle va_arg
  • Can't cast to a pointer of a struct that's typedef'd
  • Can't use complex numbers with the ternary operator

...

>

Once these final odds and ends are working, we have a killer language feature.

Even though DMD can't compile some C code, that's pretty much a non-issue for me anyway. In my environment the servers are all Linux so "apt-get" (or equivalent) typically provides a pre-compiled dependency. Being able to list a package as a system dependency and then just call it from D with no interface code is a Big Freaking Deal!

Compared to Python interfaces this is a huge improvement. It makes D an even better replacement for the mixed mode python + C development I was doing before switching to D for new projects.

April 12

On Friday, 12 April 2024 at 18:45:21 UTC, Chris Piker wrote:

>

Even though DMD can't compile some C code, that's pretty much a non-issue for me anyway. In my environment the servers are all Linux so "apt-get" (or equivalent) typically provides a pre-compiled dependency. Being able to list a package as a system dependency and then just call it from D with no interface code is a Big Freaking Deal!

Compared to Python interfaces this is a huge improvement. It makes D an even better replacement for the mixed mode python + C development I was doing before switching to D for new projects.

I agree that it's really good, but the issues I listed were just examples of the things I came across, and I know there are a decent number of other open issues for ImportC. What I'd prefer to avoid is having people try it out and concluding that it's not ready. Kind of like when I tried out Zig. Everyone was saying it's this great language but my trial was brief and I haven't been tempted to try it since.

Header files are a different story. They really should "just work" based on my experience. Other things like compiling large libraries or translating C code to D should probably be more in the camp of "experience reports" posted on individual blogs, and they should talk about both the successes and the edge cases.

« First   ‹ Prev
1 2