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

Linking with libc on Windows requires both wWinMain and WinMain #10460

Open
revivalizer opened this issue Dec 30, 2021 · 2 comments
Open

Linking with libc on Windows requires both wWinMain and WinMain #10460

revivalizer opened this issue Dec 30, 2021 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-windows standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@revivalizer
Copy link
Sponsor

Zig Version

0.10.0-dev.95+06286b05a

Steps to Reproduce

main.zig

const win32 = @import("std").os.windows;
const WINAPI = @import("std").os.windows.WINAPI;

pub export fn wWinMain(hInstance: win32.HINSTANCE, _: ?win32.HINSTANCE, pCmdLine: [*:0]u16, nCmdShow: c_int) callconv(WINAPI) c_int {
    _ = hInstance;
    _ = nCmdShow;
    _ = pCmdLine;

    return 0;
}

build.zig

    ...
    exe.linkLibC();
    exe.single_threaded = true;    
    ...

Expected Behavior

zig build success.

Actual Behavior

λ zig build
lld-link: error: undefined symbol: WinMain
>>> referenced by mingw32.lib(crt0_c.obj):(main)
error: LLDReportedFailure
libctest...The following command exited with error code 1:
C:\Users\ralph\Downloads\zig-windows-x86_64-0.10.0-dev.95+06286b05a\zig.exe build-exe C:\code\libctest\src\main.zig -lc --cache-dir C:\code\libctest\zig-cache --global-cache-dir C:\Users\ralph\AppData\Local\zig --name libctest -fsingle-threaded --enable-cache
error: the following build command failed with exit code 1:
C:\code\libctest\zig-cache\o\9f6d466e1acf870ea83a5add8bf20452\build.exe C:\Users\ralph\Downloads\zig-windows-x86_64-0.10.0-dev.95+06286b05a\zig.exe C:\code\libctest C:\code\libctest\zig-cache C:\Users\ralph\AppData\Local\zig

Note that zig does not allow me to just use WinMain:

λ zig build
C:\Users\ralph\Downloads\zig-windows-x86_64-0.10.0-dev.95+06286b05a\lib\std\start.zig:61:21: error: WinMain not supported; declare wWinMain or main instead
                    @compileError("WinMain not supported; declare wWinMain or main instead");
                    ^
libctest...The following command exited with error code 1:
C:\Users\ralph\Downloads\zig-windows-x86_64-0.10.0-dev.95+06286b05a\zig.exe build-exe C:\code\libctest\src\main.zig -lc --cache-dir C:\code\libctest\zig-cache --global-cache-dir C:\Users\ralph\AppData\Local\zig --name libctest -fsingle-threaded --enable-cache
error: the following build command failed with exit code 1:
C:\code\libctest\zig-cache\o\9f6d466e1acf870ea83a5add8bf20452\build.exe C:\Users\ralph\Downloads\zig-windows-x86_64-0.10.0-dev.95+06286b05a\zig.exe C:\code\libctest C:\code\libctest\zig-cache C:\Users\ralph\AppData\Local\zig
@revivalizer revivalizer added the bug Observed behavior contradicts documented or intended behavior label Dec 30, 2021
@revivalizer revivalizer changed the title Linking with libc on WIndows requires both wWinMain and WinMain Linking with libc on Windows requires both wWinMain and WinMain Dec 30, 2021
@Vexu Vexu added os-windows standard library This issue involves writing Zig code for the standard library. labels Dec 31, 2021
@Vexu Vexu added this to the 0.10.0 milestone Dec 31, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jun 19, 2023
@ghost
Copy link

ghost commented Oct 12, 2023

Will this be fixed before you add the support for the regular WinMain? It's really not looking good, many people use external UI libraries that require us to link libC but the console subsystem makes it much worse. It would be great if this could get fixed 😄

@DarknessFX
Copy link

I got this same error just now while trying to run SDL2 without the console window, I found a way to bypass this problem:

Zig Version

0.12.0-dev.415+5af5d87ad

Steps to Reproduce

tstwnd.zig

const std = @import("std");
const win = @import("std").os.windows;

const HINSTANCE = win.HINSTANCE;
const HWND = win.HWND;

pub export fn WinMain(hInstance: HINSTANCE, hPrevInstance: ?HINSTANCE, 
  pCmdLine: [*:0]u16, nCmdShow: u32) callconv(win.WINAPI) win.INT {

    return wWinMain(hInstance, hPrevInstance, pCmdLine, nCmdShow);
}

pub export fn wWinMain(hInstance: HINSTANCE, hPrevInstance: ?HINSTANCE, 
  pCmdLine: [*:0]u16, nCmdShow: u32) callconv(win.WINAPI) win.INT {

  _ = hInstance;
  _ = hPrevInstance;
  _ = pCmdLine;
  _ = nCmdShow;

  _ = win.user32.messageBoxA(null, "Zig run -lc tstwnd.zig", "Hello Zig!", win.user32.MB_OKCANCEL) catch 0;

  return 0;
}

command
zig run -lc tstwnd.zig

Expected Behavior

A single message box popup.

This code works now but it will fail to run if compiled without libc.
zig run tstwnd.zig

error: lld-link: root: undefined symbol: _tls_index
error: lld-link: root: undefined symbol: wWinMainCRTStartup
error: lld-link: undefined symbol: _tls_index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-windows standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

4 participants