From 8437bd12a54c6fe62c92b9a27fef03a5ec2a35cf Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 14 Sep 2024 22:28:47 +0200 Subject: Added basic Zig and C interoperability example --- zig-c-interop/Makefile | 2 ++ zig-c-interop/billy.c | 3 +++ zig-c-interop/billy.h | 1 + zig-c-interop/main.zig | 9 +++++++++ 4 files changed, 15 insertions(+) create mode 100644 zig-c-interop/Makefile create mode 100644 zig-c-interop/billy.c create mode 100644 zig-c-interop/billy.h create mode 100644 zig-c-interop/main.zig (limited to 'zig-c-interop') diff --git a/zig-c-interop/Makefile b/zig-c-interop/Makefile new file mode 100644 index 0000000..b0e2753 --- /dev/null +++ b/zig-c-interop/Makefile @@ -0,0 +1,2 @@ +default: + zig run -I. main.zig diff --git a/zig-c-interop/billy.c b/zig-c-interop/billy.c new file mode 100644 index 0000000..9544156 --- /dev/null +++ b/zig-c-interop/billy.c @@ -0,0 +1,3 @@ +#include "billy.h" + +int sum(int a, int b) { return a + b; }; diff --git a/zig-c-interop/billy.h b/zig-c-interop/billy.h new file mode 100644 index 0000000..6142d6c --- /dev/null +++ b/zig-c-interop/billy.h @@ -0,0 +1 @@ +int sum(int a, int b); diff --git a/zig-c-interop/main.zig b/zig-c-interop/main.zig new file mode 100644 index 0000000..8d16cd1 --- /dev/null +++ b/zig-c-interop/main.zig @@ -0,0 +1,9 @@ +const print = @import("std").debug.print; + +const billy = @cImport({ + @cInclude("billy.c"); +}); + +pub fn main() void { + print("Billy says 2+4={d}\n", .{billy.sum(2, 4)}); +} -- cgit v1.2.3