Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello World sample doesn't work in Windows #438

Open
ZeikJT opened this issue Feb 14, 2025 · 1 comment · May be fixed by #440
Open

Hello World sample doesn't work in Windows #438

ZeikJT opened this issue Feb 14, 2025 · 1 comment · May be fixed by #440

Comments

@ZeikJT
Copy link

ZeikJT commented Feb 14, 2025

The example given here: https://ziglang.org/learn/samples/#hello

As a beginner in zig, running it and seeing this mess wasn't fun:

lib\std\os\windows.zig:2126:13: error: unable to evaluate comptime expression
            asm (
            ^~~
lib\std\os\windows.zig:2135:15: note: called at comptime from here
    return teb().ProcessEnvironmentBlock;
           ~~~^~
lib\std\io.zig:19:27: note: called at comptime from here
        return windows.peb().ProcessParameters.hStdOutput;
               ~~~~~~~~~~~^~
lib\std\io.zig:30:40: note: called at comptime from here
    return .{ .handle = getStdOutHandle() };
                        ~~~~~~~~~~~~~~~^~
C:\Users\zeik\Documents\zig-testing\test.zig:3:32: note: called at comptime from here
const stdout = std.io.getStdOut().writer();
               ~~~~~~~~~~~~~~~~^~
C:\Users\zeik\Documents\zig-testing\test.zig:3:41: note: initializer of container-level variable must be comptime-known
const stdout = std.io.getStdOut().writer();
               ~~~~~~~~~~~~~~~~~~~~~~~~~^~

I usually assume hello world is the baseline for getting started and this was a shock. Had I misconfigured something? How? zig was a single executable, the sample on the landing page which was much more complicated worked fine.

I eventually dug up an old closed issue that explained the problem: ziglang/zig#6845 (comment)
The solution was really simple, if a bit gross, and moving that getStdOut call into the function did indeed fix the issue.

But might be worth mentioning in a note on the page or having both examples? zig beginners will struggle unnecessarily otherwise. Thanks!

EDIT: Just noticed that the Zigg Zagg example does in fact have this done in the Windows-friendly way, inside the function: https://ziglang.org/learn/samples/#zigg-zagg

@ajferrario
Copy link

For anyone looking to copy-paste the working code.

const std = @import("std");

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

jobansd added a commit to jobansd/www.ziglang.org that referenced this issue Feb 21, 2025
Fixes: [issue ziglang#438](ziglang#438)

- Hello World sample doesn't work in Windows [(Issue ziglang#438)](ziglang#438)
- Declaring a const variable outside the main function fails on target x86_64-windows-gnu [(Issue #6845)](ziglang/zig#6845 (comment))

`getStdOut()` only works at runtime on Windows. This is resolved by moving the `getStdOut()` call into the main function's scope. Additionally, [`ziggzagg.zig`](https://ziglang.org/learn/samples/#zigg-zagg) example on the same page uses the correct approach.
jobansd added a commit to jobansd/www.ziglang.org that referenced this issue Feb 21, 2025
Fixes: [issue ziglang#438](ziglang#438)

- Hello World sample doesn't work in Windows [(Issue ziglang#438)](ziglang#438)
- Declaring a const variable outside the main function fails on target x86_64-windows-gnu [(Issue #6845)](ziglang/zig#6845 (comment))

`getStdOut()` only works at runtime on Windows. This is resolved by moving the `getStdOut()` call into the main function's scope. Additionally, [`ziggzagg.zig`](https://ziglang.org/learn/samples/#zigg-zagg) example on the same page uses the correct approach.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants