Skip to content

[clang] main implicitly calls __main When building for w64-windows-gnu with no stdlib #110910

@PhilipColman

Description

@PhilipColman

Was building a project for w64-windows-gnu with std no lib my project was getting getting into a infinite loop, I was using the default entry of __main, doing so setting up and then calling main, but main was never getting call and setup was stuck in infinite loop, turns main was being call but the first thing it was doing was implicitly call __main, I found this out when I renamed my entry to start using -Wl,--entry,start if I defined an function called main I would a get undefined reference to `__main' no function called main it was fine, after this I renamed main and the issues went away.

At first i was cross complainer w64-windows-gnu from Linux but when i started facing these issues I then tried building on windows using the gnu tools as it was the same.

Both of these examples are complied with clang ./main.c --target=x86_64-w64-windows-gnu -nostdlib -Wl,--entry,start

void start() {
}

This will compile fine no issues

int main() {  // By add this line program will now try to call __main when main is called
    return 0;
}

void start() {
    main();
}

This one will get the linker error undefined reference to `__main'

You get the same undefined reference to `__main' when compiler with clang trunk on compiler explorer https://godbolt.org/z/65sYsM171

This is version of clang i am running on Linux
clang version 18.1.8
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

And this is version of clang i am running on windows
clang version 18.1.8
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/ucrt64/bin

I have also downloaded LLVM-19.1.0-win64.exe and installed on my windows system and tried building with clang 19.1 and it was the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other categoryplatform:windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions