Archived
3
0

An example of working function exports.

This commit is contained in:
Philippe Pittoli 2023-01-12 17:31:51 +01:00
parent f3c7695462
commit 614e972b95
3 changed files with 83 additions and 0 deletions

32
zig-impl/drop/Makefile Normal file
View File

@ -0,0 +1,32 @@
ZIGC=zig
CC=gcc
CFLAGS=-Wall -Wextra
LDFLAGS=-I build/ -L build/ -lipc
all: zigcompilation compilation
ifeq ($(SRC),)
test-src:
@echo SRC must be set via command line.
@exit 1
else
test-src:
endif
list-obj-files: test-src
@# List all .o included in a .a archive.
ar t $(SRC)
list-symbols: test-src
@# List all symbols in a .so.
nm -D $(SRC)
list-symbols-alt: test-src
@# Alternative: grep .text section in an objdump output.
objdump -T $(SRC) | grep text
zigcompilation: build.zig src/*.zig
$(ZIGC) build
compilation: src/main.c
@echo the following compilation will produce errors despite actually working
$(CC) -o app -o main build/libipc.so $(CFLAGS) $^ $(LDFLAGS)

23
zig-impl/drop/build.zig Normal file
View File

@ -0,0 +1,23 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const lib = b.addStaticLibrary("ipc", "src/main.zig");
lib.setOutputDir("build");
lib.setBuildMode(mode);
lib.install();
const solib = b.addSharedLibrary("ipc", "src/main.zig", b.version(0, 0, 1));
solib.setOutputDir("build");
solib.setBuildMode(mode);
solib.install();
const main_tests = b.addTest("src/main.zig");
main_tests.setBuildMode(mode);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
}

View File

@ -0,0 +1,28 @@
const std = @import("std");
const testing = std.testing;
const SOMESTRUCT = packed struct {
somevalue: i32,
const Self = @This();
fn update(self: *Self) callconv(.C) i32 {
self.somevalue += 1;
return self.somevalue;
}
};
export fn some_struct_bidouillage_init() callconv(.C) SOMESTRUCT {
return SOMESTRUCT {.somevalue = 2};
}
export fn some_struct_bidouillage_update(s: *SOMESTRUCT) callconv(.C) i32 {
return s.update();
}
export fn someipc(a: i32, b: i32) callconv(.C) i32 {
return a + b;
}
test "basic add functionality" {
try testing.expect(someipc(3, 7) == 10);
}