Hi, I'm sorry for the noise if this is not a bug, I just don't understand why the code below fails to cross-compile.
Why one should declare a const variable inside a function and not outside?
shell
root@zigbuild:/root# uname -a
Linux doud-blade 4.18.0-25-generic #26-Ubuntu SMP Mon Jun 24 09:32:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
root@zigbuild:/root# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
shell
root@zigbuild:/root# ./zig/build/zig env
{
"zig_exe": "/root/zig/build/zig",
"lib_dir": "/root/zig/build/lib/zig",
"std_dir": "/root/zig/build/lib/zig/std",
"global_cache_dir": "/root/.cache/zig",
"version": "0.6.0+e3fed3c81"
}
With const stdout = std.io.getStdOut().writer(); inside main(), which is from the documentation, compiling works for Linux and Windows
````zig
root@zigbuild:/root# cat hello.zig
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.writeAll("Hello World!\n");
}
shell
root@zigbuild:/root# ./zig/build/bin/zig build-exe hello.zig --name hello -target x86_64-linux-gnu
root@zigbuild:/root# echo $?
0
root@zigbuild:/root# ./hello
Hello World!
shell
root@zigbuild:/root# ./zig/build/bin/zig build-exe hello.zig --name hello -target x86_64-windows-gnu
root@zigbuild:/root# echo $?
0
root@zigbuild:/root# wine64 ./hello.exe
Hello World!
With `const stdout = std.io.getStdOut().writer();` **outside** `main()`, compiling works for Linux but **not Windows**
zig
root@zigbuild:/root# cat hello.zig
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
try stdout.writeAll("Hello World!\n");
}
shell
root@zigbuild:/root# ./zig/build/bin/zig build-exe hello.zig --name hello -target x86_64-linux-gnu
root@zigbuild:/root# echo $?
0
root@zigbuild:/root# ./hello
Hello World
shell
root@zigbuild:/root# ./zig/build/bin/zig build-exe hello.zig --name hello -target x86_64-windows-gnu
./zig/build/lib/zig/std/os/windows.zig:1442:20: error: unable to evaluate constant expression
.x86_64 => asm volatile (
^
./zig/build/lib/zig/std/os/windows.zig:1455:15: note: called from here
return teb().ProcessEnvironmentBlock;
^
./zig/build/lib/zig/std/io.zig:45:30: note: called from here
return os.windows.peb().ProcessParameters.hStdOutput;
^
./zig/build/lib/zig/std/io.zig:59:34: note: called from here
.handle = getStdOutHandle(),
^
./hello.zig:2:32: note: called from here
const stdout = std.io.getStdOut().writer();
^
./hello.zig:2:41: note: called from here
const stdout = std.io.getStdOut().writer();
^
./zig/build/lib/zig/std/os/windows.zig:1455:15: note: referenced here
return teb().ProcessEnvironmentBlock;
^
./zig/build/lib/zig/std/io.zig:45:30: note: referenced here
return os.windows.peb().ProcessParameters.hStdOutput;
^
./zig/build/lib/zig/std/io.zig:59:34: note: referenced here
.handle = getStdOutHandle(),
^
./hello.zig:2:32: note: referenced here
const stdout = std.io.getStdOut().writer();
^
./hello.zig:5:9: note: referenced here
try stdout.writeAll("Hello World!\n");
^
./zig/build/lib/zig/std/start.zig:326:40: note: referenced here
const result = root.main() catch |err| {
^
````
The error is sadly correct because getStdOut() only works at runtime on Windows.
Hi, I'm sorry for the noise if this is not a bug, I just don't understand why the code below fails to cross-compile.
On POSIX systems, stdout is always 1: you know this at all times and can hard-code it into the binary.
On windows, stdout needs to be fetched from the PEB at runtime. But once you fetch it, you know it (i.e. it's constant).
Why one should declare a const variable inside a function and not outside?
You can declare outside only if it's known at compile time; otherwise you need to do it inside a function.
Most helpful comment
On POSIX systems, stdout is always
1: you know this at all times and can hard-code it into the binary.On windows, stdout needs to be fetched from the PEB at runtime. But once you fetch it, you know it (i.e. it's constant).
You can declare outside only if it's known at compile time; otherwise you need to do it inside a function.