Thread overview
Creating a .di file for a custom C library
Mar 30, 2021
Brad
Mar 30, 2021
evilrat
Mar 30, 2021
Chris Piker
Mar 30, 2021
Ferhat Kurtulmuş
March 30, 2021
I would like to use an updated version of the Termbox library (written in C) with D.  I have the .h file.  This is new territory for me (why try something easy - right?).  I think I need to create a .di file that corresponds to the .h file.  I also suspect that I need to put the library file (C code) into the project as a file somehow.  I am probably just not looking in the right place for the documentation, but I cannot seem to find a lot of guidance in this area.

Thanks in advance.
March 30, 2021
On Tuesday, 30 March 2021 at 04:01:12 UTC, Brad wrote:
> I would like to use an updated version of the Termbox library (written in C) with D.  I have the .h file.  This is new territory for me (why try something easy - right?).  I think I need to create a .di file that corresponds to the .h file.  I also suspect that I need to put the library file (C code) into the project as a file somehow.  I am probably just not looking in the right place for the documentation, but I cannot seem to find a lot of guidance in this area.
>
> Thanks in advance.

No just convert C signatures to corresponding D signatures in regular .d file.
Then you need to build original C library, and then when building D program you link(pass produced .lib/.a files to compiler/linker) this stuff with C library.

After all your binaries is just language agnostic bytes, however there is calling conventions that exist for interop, your .h contains definitions (aka contract) of what it does, and produced binaries (.exe, .a, .lib, .dll, .so) contains actual machine code, so on D side you must match that contract and then tell the linker to embed the machine code from .a/.lib in your final executable/DLL.
March 30, 2021
On Tuesday, 30 March 2021 at 04:01:12 UTC, Brad wrote:
> I would like to use an updated version of the Termbox library (written in C) with D.  I have the .h file.  This is new territory for me (why try something easy - right?).  I think I need to create a .di file that corresponds to the .h file.  I also suspect that I need to put the library file (C code) into the project as a file somehow.  I am probably just not looking in the right place for the documentation, but I cannot seem to find a lot of guidance in this area.
>
> Thanks in advance.

Hi Brad

I used dstep https://code.dlang.org/packages/dstep to help me with this process.  Though I ended up checking the output and fixing a couple oddities in the "extern (C):" line, it was *huge* time saver.

The result was a D module file (i.e. thing.d, not thing.di) that I could include like any other, so long as the C library was specified on the compiler command line.

In addition, you might want to check out https://wiki.dlang.org/Deimos where standard patterns are discussed for D prototypes of C libraries.  I ended up using the command line:

  dub init --format=deimos

to start off the wrapper lib package, then copied in the C header, and then ran dstep.

Cheers,

March 30, 2021
On Tuesday, 30 March 2021 at 04:01:12 UTC, Brad wrote:
> I would like to use an updated version of the Termbox library (written in C) with D.  I have the .h file.  This is new territory for me (why try something easy - right?).  I think I need to create a .di file that corresponds to the .h file.  I also suspect that I need to put the library file (C code) into the project as a file somehow.  I am probably just not looking in the right place for the documentation, but I cannot seem to find a lot of guidance in this area.
>
> Thanks in advance.

I never needed or used .di files. Dstep[1] can create d modules containing c header definitions. Recently, I used to create a d binding [2] for shapelib[3]. I just added "module shapelib;" at the beginning of the file, nothing else. Some c headers may require extra steps IMO.


1: https://github.com/jacob-carlborg/dstep
2: https://github.com/aferust/shapelib-d/blob/main/shapelib.d
3: https://github.com/OSGeo/shapelib