Example code: first steps towards C bindings.

master
Philippe Pittoli 2023-01-13 02:05:17 +01:00
parent aca3f2183d
commit 32fd31934c
2 changed files with 18 additions and 2 deletions

View File

@ -7,11 +7,13 @@ pub fn build(b: *std.build.Builder) void {
const lib = b.addStaticLibrary("ipc", "src/main.zig");
lib.setOutputDir("build");
lib.linkLibC();
lib.setBuildMode(mode);
lib.install();
const solib = b.addSharedLibrary("ipc", "src/main.zig", b.version(0, 0, 1));
solib.setOutputDir("build");
solib.linkLibC();
solib.setBuildMode(mode);
solib.install();

View File

@ -1,6 +1,8 @@
const std = @import("std");
const testing = std.testing;
const print = std.debug.print;
const SOMESTRUCT = packed struct {
somevalue: i32,
const Self = @This();
@ -11,8 +13,20 @@ const SOMESTRUCT = packed struct {
}
};
export fn some_struct_bidouillage_init() callconv(.C) SOMESTRUCT {
return SOMESTRUCT {.somevalue = 2};
export fn some_struct_bidouillage_init(ptr: *anyopaque) callconv(.C) void {
var pointer = @ptrCast(*SOMESTRUCT, @alignCast(@alignOf(SOMESTRUCT),ptr));
var somestruct = std.heap.c_allocator.create(SOMESTRUCT) catch null;
print ("hello we just did something\n", .{});
if (somestruct) |s| {
s.somevalue = 2;
print ("just changed a value\n", .{});
pointer.* = s.*;
}
print ("hello again\n", .{});
// else {
// pointer.* = null;
// }
}
export fn some_struct_bidouillage_update(s: *SOMESTRUCT) callconv(.C) i32 {