Hi all,
This is my first post so I will introduce myself a little bit.
I’m working on machine vision for industrial applications mostly using C/C++. When I heard about Zig I saw that you didn’t need to use CMake I thought this was something worth to try. And it was, I love it.
So far I’m using it as a test & build system and everything has been smooth till now. I’m having a seg fault with the following code:
const std = @import("std");
const neurala = @cImport(@cInclude("neurala/api/c/inspectorlib.h"));
pub fn main() void {
const model_path = "D:/Back_v2.zip";
var model: ?*neurala.neurala_brain = undefined;
std.debug.print("Loading model...", .{});
const status = neurala.neurala_create_brain_from_path(model_path, &model);
defer neurala.neurala_free_brain(model);
if (status == neurala.NEURALA_ERRC_OK) {
std.debug.print("Loading was OK", .{});
} else {
std.debug.print("Got error: {d} ", .{status});
}
}
If I do the same in C compiling it with Zig, it works fine:
int main(int argc, char **argv) {
char *model_path = "D:\\Back_v2.zip";
printf("Loading Model: %s\n", model_path);
neurala_brain* brain;
neurala_error_code code = neurala_create_brain_from_path(model_path, &brain);
if (code == NEURALA_ERRC_OK) {
printf("Model loaded sucessfully: %s\n", model_path);
}
else {
printf("Error loading model: %i\n", code);
}
neurala_free_brain(brain);
}
And the build.zig file is:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const c_example = b.addExecutable(.{
.name = "c_example",
.target = target,
.optimize = optimize,
});
c_example.addCSourceFile(.{ .file = .{ .path = "src/example_api.c" } });
c_example.addIncludePath(.{ .path = "C:/Program Files/Neurala/InspectorLib/include" });
c_example.addLibraryPath(.{ .path = "C:/Program Files/Neurala/InspectorLib/lib" });
c_example.linkSystemLibrary("NeuralaInspectorLib");
c_example.linkLibC();
b.installArtifact(c_example);
const zig_example = b.addExecutable(.{ .name = "zig_example", .target = target, .optimize = optimize, .root_source_file = .{ .path = "src/example_api.zig" } });
zig_example.addIncludePath(.{ .path = "C:/Program Files/Neurala/InspectorLib/include" });
zig_example.addLibraryPath(.{ .path = "C:/Program Files/Neurala/InspectorLib/lib" });
zig_example.linkSystemLibrary("NeuralaInspectorLib");
zig_example.linkLibC();
b.installArtifact(zig_example);
}
The error I get when I execute the zig_example.exe is:
PS D:\Devel\Neurala\zig_test\zig-out\bin> .\zig_example.exe
Loading model...Segmentation fault at address 0x4
???:?:?: 0x7ffd0f1a4a3c in ??? (KERNELBASE.dll)
???:?:?: 0x7ffcc0a9d71b in ??? (gna.dll)
???:?:?: 0x7ffcc0a9d3a9 in ??? (gna.dll)
???:?:?: 0x7ffcc0aa034f in ??? (gna.dll)
???:?:?: 0x7ffcc0aa1ace in ??? (gna.dll)
???:?:?: 0x7ffcc0aa1b6e in ??? (gna.dll)
???:?:?: 0x7ffcc0a0c461 in ??? (gna.dll)
???:?:?: 0x7ffcc0a0c3d3 in ??? (gna.dll)
???:?:?: 0x7ffcc0a0c2ef in ??? (gna.dll)
???:?:?: 0x7ffcc0a0a6e1 in ??? (gna.dll)
???:?:?: 0x7ffcc0a0ee5e in ??? (gna.dll)
???:?:?: 0x7ffcc0b27e0d in ??? (???)
???:?:?: 0x7ffcc0c61ba5 in ??? (???)
???:?:?: 0x7ffcc0c62fd1 in ??? (???)
???:?:?: 0x7ffcc0c67f93 in ??? (???)
???:?:?: 0x7ffcc0c4afc1 in ??? (???)
???:?:?: 0x7ffcc0c48792 in ??? (???)
???:?:?: 0x7ffcc66fb8b6 in ??? (???)
???:?:?: 0x7ffcc66f9adb in ??? (???)
???:?:?: 0x7ffce951ffac in ??? (???)
???:?:?: 0x7ffce9521154 in ??? (???)
???:?:?: 0x7ffcc80ad1f0 in ??? (???)
???:?:?: 0x7ffcc80ae2fd in ??? (???)
???:?:?: 0x7ffcc80af78d in ??? (???)
???:?:?: 0x7ffcc80b0720 in ??? (???)
???:?:?: 0x7ffcc8150f10 in ??? (???)
???:?:?: 0x7ffcc815938b in ??? (???)
???:?:?: 0x7ffcc8161176 in ??? (???)
???:?:?: 0x7ffcc821ccb6 in ??? (???)
???:?:?: 0x7ffcc82222dc in ??? (???)
???:?:?: 0x7ffcc821a130 in ??? (???)
???:?:?: 0x7ffcc8206fd3 in ??? (???)
???:?:?: 0x7ffce9413b6c in ??? (???)
???:?:?: 0x7ffce941770e in ??? (???)
D:\Devel\Neurala\zig_test\src\example_api.zig:9:58: 0xc013df in main (zig_example.exe.obj)
const status = neurala.neurala_create_brain_from_path(model_path, &model);
In the header, the neurala_brain type is defined as:
typedef void* neurala_brain;
I think it’s related to how I’ve defined the model on the zig file but I don’t know how to do it.
var model: ?*neurala.neurala_brain = undefined;
Do you have any idea how I can get more information or how to solve it?
Thank you comunity!
Genís
EDIT: I’m debugging both executables (zig and c examples) and it seems ,in zig, the model variable appears as undefined whereas the c example has a valid address. Can this be related to some optimization on the zig executable?