On Thursday, 20 February 2025 at 20:09:29 UTC, Ian wrote:
> 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)
Ian, You do not need importC in order to interface with a C library. A good starting read would be: https://dlang.org/spec/interfaceToC.html
If you know the C code (or C library you want to interface with) it is relatively easy to use it directly from D. The real challenge sometimes is to "port" complex C macros to D. The following page is an essential read: https://dlang.org/articles/ctod.html
Here is a simple start project. Let's call liblz4's LZ4_versionNumber() function to get the version of the library, and print it out:
module lz4version;
import std.stdio;
// https://github.com/lz4/lz4/blob/dev/lib/lz4.h line 142
extern(C) int LZ4_versionNumber();
int main() {
int lz4Version = LZ4_versionNumber();
int m = lz4Version / 10000;
lz4Version = lz4Version % 10000;
int n = lz4Version / 100;
lz4Version = lz4Version % 100;
int r = lz4Version;
writeln("LZ4 library version: ", m, ".", n, ".",r);
return 0;
}
Save this code to the lz4version.d file, and let's compile and link to lz4version executable with:
gdc -o lz4version lz4version.d -llz4
Now, let's run it:
shell» ./lz4version
LZ4 library version: 1.9.4
Indeed that is the version of the LZ4 library I have on my local Fedora 40 workstation: /usr/lib64/liblz4.so.1.9.4