Example code: first steps towards C bindings.
parent
aca3f2183d
commit
32fd31934c
|
@ -7,11 +7,13 @@ pub fn build(b: *std.build.Builder) void {
|
||||||
|
|
||||||
const lib = b.addStaticLibrary("ipc", "src/main.zig");
|
const lib = b.addStaticLibrary("ipc", "src/main.zig");
|
||||||
lib.setOutputDir("build");
|
lib.setOutputDir("build");
|
||||||
|
lib.linkLibC();
|
||||||
lib.setBuildMode(mode);
|
lib.setBuildMode(mode);
|
||||||
lib.install();
|
lib.install();
|
||||||
|
|
||||||
const solib = b.addSharedLibrary("ipc", "src/main.zig", b.version(0, 0, 1));
|
const solib = b.addSharedLibrary("ipc", "src/main.zig", b.version(0, 0, 1));
|
||||||
solib.setOutputDir("build");
|
solib.setOutputDir("build");
|
||||||
|
solib.linkLibC();
|
||||||
solib.setBuildMode(mode);
|
solib.setBuildMode(mode);
|
||||||
solib.install();
|
solib.install();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const print = std.debug.print;
|
||||||
|
|
||||||
const SOMESTRUCT = packed struct {
|
const SOMESTRUCT = packed struct {
|
||||||
somevalue: i32,
|
somevalue: i32,
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
@ -11,8 +13,20 @@ const SOMESTRUCT = packed struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export fn some_struct_bidouillage_init() callconv(.C) SOMESTRUCT {
|
export fn some_struct_bidouillage_init(ptr: *anyopaque) callconv(.C) void {
|
||||||
return SOMESTRUCT {.somevalue = 2};
|
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 {
|
export fn some_struct_bidouillage_update(s: *SOMESTRUCT) callconv(.C) i32 {
|
||||||
|
|
Reference in New Issue