We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3346

Summary: undefined reference to 'WinMain@16'
Product: SDL Reporter: Matheus Vieira <matheusvf1234>
Component: buildAssignee: Sam Lantinga <slouken>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: critical    
Priority: P2 CC: admin, philipp.wiesemann
Version: HG 2.1   
Hardware: x86_64   
OS: Windows (All)   
Attachments: Fixing the bug

Description Matheus Vieira 2016-05-26 23:18:47 UTC
Created attachment 2467 [details]
Fixing the bug

I was getting the error " undefined reference to 'WinMain@16' " só after a looking at the answer of the user X-Frox at this link:

http://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16 

I had the idea to change "#if defined(__WIN32__)" to "#if defined(__WIN__)" at the SDL_main.h end everything worked fine.

Probably if your windows is 32 bits you won't get this error.
Comment 1 Philipp Wiesemann 2016-05-27 19:46:37 UTC
__WIN32__ is a macro defined by SDL for 32 bit and 64 bit (in "SDL_platform.h").

The problem with WinMain() might be fixed by accident with __WIN__ if this macro is not defined. This would lead to the same result like defining SDL_MAIN_HANDLED before including "SDL.h" (as pointed out in the linked Stack Overflow post).
Comment 2 Sam Lantinga 2016-10-01 18:44:37 UTC
It looks like this can be resolved by checking to make sure you're defining main() properly and including SDL.h in that file.
Comment 3 Vitaly Novichkov 2019-12-01 20:49:04 UTC
Okay, can you clarify the error more detailed? I still have this error, look:

- We have a MinGW or MinGW-w64 toolchain

- We have a simple code called as "meow.c"
```
#include "SDL.h"
#include "SDL_log.h"

int main(int argc, char *arvg[])
{
    SDL_Log("Meow!");
    return 0;
}
```

- We have the latest SDL2 built via CMake (or by AutoTools)

- Trying to build it by the next command:
```
>gcc meow.c -o meow.exe -IWhereIsSDL/include/SDL2 -LWhereIsSDL/lib -lSDL2main -lSDL2 -mconsole
```

And the result is:
```
lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e):
undefined reference to `WinMain'
```

Can anyone HERE explain how to CORRECTLY declare the main function and how to link it to make SDLmain work?
Comment 4 Sam Lantinga 2019-12-04 06:32:44 UTC
This is a different error, but just by looking at it, it seems you might be able to fix it like this:
gcc meow.c -o meow.exe -IWhereIsSDL/include/SDL2 -LWhereIsSDL/lib -lmingw32 -lSDL2main -lSDL2 -mconsole

It's interesting that it's missing WinMain instead of WinMain@16, which means it's probably using cdecl instead of stdcall calling convention. SDL currently doesn't support that, but it would be easy to add if that's the case.

Try editing src/main/windows/SDL_windows_main.c and adding this at the bottom:
int
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
    return main_getcmdline();
}

and let us know if that works?
Comment 5 Vitaly Novichkov 2019-12-04 11:01:36 UTC
Going to try that...

BTW: I think this should also work in MSVC too I guess...
Comment 6 Vitaly Novichkov 2019-12-04 11:38:42 UTC
Okay, that doesn't matter, the -lmingw32 contains another "WinMain" symbol with a different code that is needed for MinGW itself. So, we should document that for MinGW case we should link "-lmingw32" before SDL2main and SDL2 for a correct work of thing.