Emitting a static/dynamic library fails if use the print

Hi all,

I am trying to create a library either static or dynamic.

I followed the official doc:

The problem is, if I use the std.debug.print or std.log.info, the build fails without much information.

The error log looks like this (please kindly ignore unrelated info, like the path):

$ zig build --summary all
└─ install hello
   └─ zig build-lib hello Debug native failure
error: the following command terminated unexpectedly:
/home/bus710/zig/zig build-lib -ODebug -Mroot=/home/bus710/repo/zigws/src/p99-go-interop/hello.zig -femit-h --cache-dir /home/bus710/repo/zigws/src/p99-go-interop/zig-cache --global-cache-dir /home/bus710/.cache/zig --name hello -static --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install hello transitive failure
   └─ zig build-lib hello Debug native failure
error: the following build command failed with exit code 1:
/home/bus710/repo/zigws/src/p99-go-interop/zig-cache/o/afce53368a0985c7c9bf9b6d3bc50e19/build /home/bus710/zig/zig /home/bus710/repo/zigws/src/p99-go-interop /home/bus710/repo/zigws/src/p99-go-interop/zig-cache /home/bus710/.cache/zig --seed 0x6d6a16be -Z987801a6cc3cb6ac --summary all

Does anyone know if this is an intended behavior or something else?
I am using 0.12.0-dev.3152+90c1a2c41 in Debian x86_64.

Good question - I recently ran into some weird behavior with printing from device kernels so maybe (very unlikely) there’s something I can connect here.

Looking at the debug call, I found that it writes to stdErr:

/// Print to stderr, unbuffered, and silently returning on failure. Intended
/// for use in "printf debugging." Use `std.log` functions for proper logging.
pub fn print(comptime fmt: []const u8, args: anytype) void {
    defer stderr_mutex.unlock();
    const stderr = io.getStdErr().writer();
    nosuspend stderr.print(fmt, args) catch return;

It also looks like logging functions write to stdErr by default:

pub fn defaultLog(
    comptime message_level: Level,
    comptime scope: @Type(.EnumLiteral),
    comptime format: []const u8,
    args: anytype,
) void {
    const level_txt = comptime message_level.asText();
    const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
    const stderr = std.io.getStdErr().writer();
    var bw = std.io.bufferedWriter(stderr);
    const writer = bw.writer();

    defer std.debug.getStderrMutex().unlock();
    nosuspend {
        writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
        bw.flush() catch return;

Might be totally aside the issue, but can you try to write directly to stdOut? I’d like to see the result.

1 Like

I tried few combinations if there is any luck, but nothing worked for me:

const std = @import("std");

var stderr_mutex = std.Thread.Mutex{};

export fn hello() void {
    // stderr_mutex.lock();
    // defer stderr_mutex.unlock();
    // const stderr = io.getStdErr().writer();
    const stderr = std.io.getStdErr().writer();
    const bw = std.io.bufferedWriter(stderr);
    _ = bw;

As soon as I put the -femit-h or getEmittedH() to generate the header, the build process gets terminated. Without those options, I could create the object file (well, no header file).

Okay, so I think the next step is to post your build.zig file here so we can all take a look at it.

For some unknown reason, requires linking with libc.
Without linking to libc results to: terminated by signal SIGSEGV (Address boundary error)

Add -lc in zig build-lib or lib.linkLibC() in build.zig


That’s what I was going to look for, actually. I suspected something like that when @bus710 said::

If you add the lines that @dimdin recommended and you still have a problem, post your build here and we can look at it further.


I was able to build a static library object and a header file by adding the linkLibC function in my build.zig file.

I tried to use it from Go, but got a bunch of error since it doesn’t know where the zig.h is. I added to link it, then it says ZIG_TARGET_MAX_INT_ALIGNMENT is undeclared.

I guess being able to build my code as library is the very first step, and now I need to dig deeper to do more. Since now I can generate the header that can print (zig-zig situation, not go-zig situation), let me close this topic.

Thanks for @AndrewCodeDev, @dimdin.