Skip to content

-lc on wasm32-wasi-musl without pub fn main and with -fno-entry causes linker failure about not including proper main symbol #22570

Closed as not planned

Description

@MichaelBelousov

Zig Version

0.14.0-dev.2851+b074fb7dd

Steps to Reproduce and Observed Behavior

This also occurs in 0.13.0 I just noticed.

  1. create file main.zig:
export fn func() u32 {
    return 5;
}
  1. build with command:
zig build-exe --export=func -fno-entry -lc -target wasm32-wasi-musl main.zig
  1. I received the following output:
b22ac386e1/libc.a(/home/mike/.cache/zig/o/bdbc5e1f927e7d98a8e9ea2611c26b68/__main_void.o): undefined symbol: main

Expected Behavior

for an error about not including a pub fn main to surface instead of a linker error.

And it should just compile with -fno-entry passed

Activity

  1. added
    bug
    on Jan 22, 2025
  2. added
    arch-wasm
    os-wasi
    on Jan 22, 2025
  3. added this to the 0.15.0 milestone on Jan 22, 2025
  4. rohlem commented on Jan 22, 2025

    @rohlem
  5. chearon commented on Feb 9, 2025

    @chearon

    I think maybe this file supposed to be conditionally added to the compile (not if .entry = .disabled):

    "wasi/libc-bottom-half/sources/__main_void.c",

    As OP's output shows, the symbol is referenced in that file:

    int __wasilibc_main(int argc, char *argv[]) asm("main");

    The easy workaround is to stub out main. In my case, I'm building a C++ library to WASM, so I included this:

    // wasi produces a reference to this in __main_void.c even though the linker
    // won't call it with .entry = .disabled
    int
    main() {
      return 0;
    }

    That also removes unwanted "args_get" and "args_sizes_get" imports in the final .wasm.

  6. removed this from the urgent milestone on Oct 4, 2026
  7. added a commit that references this issue on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmbuglinkingos-wasi

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions