Zig: Subsystem flag on zig-build command line is ignored, depending on program contents

Created on 28 May 2019  路  5Comments  路  Source: ziglang/zig

The --subsystem flag on zig-build gets clobbered by the subsystem auto-detection code.

If you have a main function, then zig forces -SUBSYSTEM:console in the linker. If you have a WinMain function then it forces -SUBSYSTEM:windows in the linker.

I believe this is due to zig\src\analyze.cpp replacing the value passed on the command line during add_fn_export (right around line 2710).

The logic in this function is nice to have, but I think it should only provide a default value, if one isn't specified on the command line.

Repro steps:

Main.zig file:

pub const HANDLE = *c_void;
pub const LPCTSTR = [*]const u8;
pub const UINT = c_uint;
pub const HINSTANCE = *@OpaqueType();
pub const PWSTR = [*]u16;
pub const INT = c_int;

extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) c_int;

pub fn main() void {
    _ = MessageBoxA(null, c"hello", c"title", 0);
}

Compile (on windows) with:

zig build-exe --subsystem windows --verbose-link main.zig

Expected:
lld line contains -SUBSYSTEM:windows

Actual:
lld line contains -SUBSYSTEM:console

Alternate repro steps (based on modifications to previous program):

Edit the Main.zig file above. Replace the entry point function with this code:

export fn WinMain() void {
    _ = MessageBoxA(null, c"hello", c"title", 0);
}

Compile (on windows) with:

zig build-exe --subsystem console --verbose-link main.zig

Expected:
lld line contains -SUBSYSTEM:console

Actual:
lld line contains -SUBSYSTEM:windows

bug

All 5 comments

I think this is #2554 ?

fixed by @emekoi in 3f302594b855e4d0dff9fbbfb2fe9d1d1801cb6e (I just merged it)

I think this is #2554 ?

Yup. It sure is :)

Thanks for the report! Sorry I didn't get that merged in time for you to bump into the issue.

I forgot the fix was being done (or missed that it was being done). Wanted to make sure this got recorded at the very least.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dobkeratops picture dobkeratops  路  3Comments

jayschwa picture jayschwa  路  3Comments

jorangreef picture jorangreef  路  3Comments

daurnimator picture daurnimator  路  3Comments

bronze1man picture bronze1man  路  3Comments