www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Easy way for FFI

reply Maximilian Naderer <maximilian.wallinger outlook.com> writes:
Hello,
so what are my / our best options to create an automatic C 
foreign function interface.

I would like to create such interface / header file from a D 
module with public functions, structs which are declared with 
extern(C)

D code

module leo

extern(C) void test() { }

Expectation:

// header file
void leo_test(void);
Sep 22
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
There are a number of solutions for C bindings creation.

Try ImportC its built into the compiler.

You can use the .di generator to emit a D file for further use and 
manual cleanup.

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

It does depend upon the system toolchain, so you'll want MSVC installed 
if you use Windows.
Sep 22
parent reply Maximilian Naderer <maximilian.wallinger outlook.com> writes:
Hello Richard,

Thank you for the info. I thought this goes the other way around. 
Using a header file and converting it to a .di file.

I want to create a .h file from my D modules so you’re saying 
this should be also possible ?
Sep 22
parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 23/09/2024 10:13 AM, Maximilian Naderer wrote:
 I want to create a .h file from my D modules so you’re saying this 
 should be also possible ?
There is support in the compiler, but its not really mature. https://dlang.org/dmd-windows.html#switch-HC[
Sep 22