zig icon indicating copy to clipboard operation
zig copied to clipboard

Windows std.os.getenv(): lld-link: error: undefined symbol: environ

Open jcmoyer opened this issue 4 years ago • 1 comments

I have an executable that I'm try to build linked to libc on Windows. If I try to use std.os.getenv I get this linker error:

lld-link: error: undefined symbol: environ
>>> referenced by D:\...\zig-install\lib\zig\std\os.zig:1388
>>>               D:\...\zig-cache\o\4e0efec30ee2fdf8e70f3ef88ccff625\main.obj:(std.os.getenv)
error: LLDReportedFailure

From a quick glance at the source I see that I shouldn't(?) be using this function, but the @compileError didn't trigger. I'm not sure what should happen since I am linking libc and environ is apparently available on Windows (albeit with a leading underscore).

jcmoyer avatar Apr 07 '21 09:04 jcmoyer

since I am linking libc

the if statement preceding the check with the compile error checks for if you're linking libc. the body of which then references std.c.environ here https://github.com/ziglang/zig/blob/f253822415304fc069f68452f7f4abbded58a24e/lib/std/os.zig#L1388

perhaps the second if statement should be first

nektro avatar Sep 14 '21 11:09 nektro

Hey there! Dropping a note on an old issue if anyone finds it in the future like I did. I think the preferred way to get environment variables out of the stdlib is process.getEnvVarOwned, which works great for me on Windows as well as Linux, though I haven't tried any other platforms.

Might be worth re-thinking this API when it comes time to audit the stdlib (we noted another candidate over here)?

Something like:

const p = try std.process.getEnvVarOwned(allocator, "PATH");
defer allocator.free(p);
std.debug.print("{s}\n", .{p});

jcalabro avatar May 05 '23 16:05 jcalabro