skip to Main Content

I have copied a hello world Zig program and it runs fine locally on my Mac:

const std = @import("std");

pub fn main() anyerror!void {
    // Note that info level log messages are by default printed only in Debug
    // and ReleaseSafe build modes.
    std.log.info("All your codebase are belong to us.", .{});
}

test "basic test" {
    try std.testing.expectEqual(10, 3 + 7);
}

I then cross-compiled it to ARM:

zig build-exe src/main.zig -O ReleaseSmall --strip -target aarch64-linux

Seems to compile the right thing:

$ file ./main
./main: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, stripped

Then I copied it to my Raspberry Pi running Ubuntu 22.

It also shows the file is recognized and runs without error, but it just doesn’t print anything. I tried redirecting both stdout and stderr to a file but nothing comes out.

What can be the problem?

2

Answers


  1. Chosen as BEST ANSWER

    Turns out the code I had copied was not the "real" hello world. It only prints to the console when it's built in debug mode.

    The production-grade hello world in Zig is this:

    const std = @import("std");
    
    pub fn main() !void {
        const stdout = std.io.getStdOut().writer();
        try stdout.print("Hello, {s}!n", .{"world"});
    }
    

    Compiling this the same way, it works both on Mac and when compiled to aarch64-linux and executed on my Raspberry Pi.


  2. To add, std.log.info does not display in ReleaseFast or ReleaseSmall builds by default. You can change this by overriding the log level in your main zig file:

    pub const log_level: std.log.Level = .info;
    

    Or, you can print to stdout directly rather than using logging functions:

    const std = @import("std");
    
    pub fn main() !void {
        const stdout = std.io.getStdOut().writer();
        try stdout.print("Hello, world!n", .{});
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search