llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.38k stars 11.73k forks source link

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

Open PhilipColman opened 6 days ago

PhilipColman commented 6 days ago

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.

Disservin commented 6 days ago

please don't open issues for general programming related issues, checkout some programming discord maybe, if anything this would need a reproducer

PhilipColman commented 6 days ago

This is programming issues, I have resolved the issues in my project, by renaming to main, and that else resolve issues, but defining main should not implicitly __main, when main is called.

Here are some example you can use to reproduce the issues with

All 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'

Disservin commented 6 days ago

I see, just some future advice your issue description is too long to read and not nicely formatted and should always have a reproduction, I suggest you change it with your second answer, and include this godbolt, https://godbolt.org/z/65sYsM171 and change the title to maybe something like undefined reference to __main when cross compiling to windows with no stdlib, since I think that's the issue

gcc mentions this case, https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Link-Options.html#:~:text=One%20of%20the,see%20collect2.)