Intro to calling C libraries
Lance Bachmeier
no at spam.net
Thu Feb 20 21:05:40 UTC 2025
On Thursday, 20 February 2025 at 20:09:29 UTC, Ian wrote:
> Hi,
>
> What is the recommended documentation or introductory material
> on how to call C libraries from D? I've seen it done in GtkD
> (for GTK3) and I have heard of -betterC, but it's all a little
> overwhelming. (I'm an experienced C programmer but new to D)
>
> Cheers,
> Ian
Normally, you should be using ImportC. Look at the quick example
from the spec: https://dlang.org/spec/importc.html#examples
The first line of hello.c looks like this:
```
#include <stdio.h>
```
When ImportC compiles a C file, it runs the C preprocessor and
does all the including you need. But rather than stdio.h, you can
compile a C file that includes all the C headers you need. That's
all you have to do. You can use the usual preprocessor calls like
-I by using the -P switch:
https://dlang.org/spec/importc.html#auto-cpp
Assuming ImportC works with your C headers, that's everything you
need.
If ImportC doesn't work (as may be the case due to C extensions
and preprocessor hacks), you should use dpp or dstep:
https://dlang.org/spec/importc.html#dpp
More information about the Digitalmars-d-learn
mailing list