I moved my C dependency to a Zig package. It builds fine but when I try to import it into my project I’m getting an import error.
build.zig for project:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "tag",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const flags = b.dependency("flags", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("flags", flags.module("flags"));
const lodepngz = b.dependency("lodepngz", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("lodepngz", lodepngz.module("lodepngz"));
// what I was using before
// exe.addIncludePath(b.path("clib"));
// exe.addObjectFile(b.path("clib/lodepng.o"));
exe.linkSystemLibrary("turbojpeg");
exe.linkLibC();
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
build.zig.zon for project:
.{
.name = "tag",
.version = "0.5.0",
.dependencies = .{
.flags = .{
.url = "https://github.com/n0s4/flags/archive/refs/heads/main.tar.gz",
.hash = "12202e9d5de187569b77064c66a4972e8a824488295fab2f5c8cb48331eab9877257",
},
.lodepngz = .{
.url = "https://forge.ohnyo.com/coredump/lodepngz/archive/ddc283419fe9624627e1d975792c77aa61e5b496.tar.gz",
.hash = "1220f6d26b2e6e1a9ad55b816a7584f0718fa9f78bff45e78e276457e1cb1f585b9d",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
build.zig for lib:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "lodepngz",
.target = target,
.optimize = optimize,
});
const flags = [_][]const u8{
"-ansi",
"-O3",
};
lib.linkLibC();
lib.addIncludePath(b.path("lodepng/lodepng.h"));
lib.addCSourceFile(.{.file = b.path("lodepng/lodepng.c"), .flags = &flags});
b.installArtifact(lib);
}
build.zig.zon for lib:
.{
.name = "lodepngz",
.version = "1.0.0",
.paths = .{"."},
}
error on compilation:
thread 32391 panic: unable to find module 'lodepngz'
/usr/lib/zig/lib/std/Build.zig:1896:18: 0x115bd07 in module (build)
panic("unable to find module '{s}'", .{name});
^
/home/wizard/fun/zig/learn/tag/build.zig:27:58: 0x1115f6f in build (build)
exe.root_module.addImport("lodepngz", lodepngz.module("lodepngz"));
^
/usr/lib/zig/lib/std/Build.zig:2155:33: 0x10fb2f3 in runBuild__anon_8801 (build)
.Void => build_zig.build(b),
^
/usr/lib/zig/lib/compiler/build_runner.zig:301:29: 0x10f649f in main (build)
try builder.runBuild(root);
^
/usr/lib/zig/lib/std/start.zig:515:37: 0x10ddea5 in posixCallMainAndExit (build)
const result = root.main() catch |err| {
^
^
/usr/lib/zig/lib/std/start.zig:258:5: 0x10dd9c1 in _start (build)
asm volatile (switch (native_arch) {
^
???:?:?: 0x8 in ??? (???)
Unwind information for `???:0x8` was not available, trace may be incomplete
I need to compile the C lib in O3 otherwise it segfaults. I tried to not do this but it needs it. It is in the library’s build instructions to build with -O3
and -ansi
. What am I doing wrong in regards to importing this library? I usually follow this pattern when importing pure Zig libraries. Do C dependent libraries require more steps?
before the change I would have to run the following if you are on a new machine:
cd clib
zig cc -ansi -c -O3 lodepng.c -o lodepng.o
zig build -Doptimize=ReleaseFast
I’m trying to get a clean build flow. By just running, zig build -Doptimize=ReleaseFast
You can wget the file in the zon file and it compiles fine:
[wizard@thinkchad tag]$ wget https://forge.ohnyo.com/coredump/lodepngz/archive/ddc283419fe9624627e1d975792c77aa61e5b496.tar.gz
[wizard@thinkchad tag]$ tar xf ddc283419fe9624627e1d975792c77aa61e5b496.tar.gz
[wizard@thinkchad tag]$ cd lodepngz/
[wizard@thinkchad lodepngz]$ zig build -Doptimize=ReleaseFast
[wizard@thinkchad lodepngz]$