A recent thread on mingw-users mailing list brought the following
problems up when linking a SDL application:
- ‘main’ function is overridden in a way that subverts a typical valid
C ‘main’ entry function. - By overwriting ‘main’, linkage and linking library order is enforced
in a non-natural way. - sdlmain is called conditionally depending on how a user links his
application.
I’m proposing and strongly encouraging to remove the ‘main’/'sdlmain’
overloading in SDL 1.3 since I see only problems associated with it, and
few benefits compared to current initialization of SDL and improving it.
Knowing that I’ve noticed that some initialization, depending on
architecture, seems to be necessary (such as BeOS); here are
nevertheless my points:
-
‘main’ is standard C function whose prototype is exactly:
int main(int, char**, char**).
Because of C linkage and the non-enforcement of C parameter types, we
usually just use ‘int main(int, char**)’; but any parameter list should
be valid and accepted. SDL (SDL_main.h precisely) currently denies this.
The only requirement in C is that ‘main’ returns ‘int’. -
Because of redefining ‘main’ to ‘sdlmain’, linking a SDL application
(under mingw) becomes “gcc foobar.c -lmingw32 -lSDLmain -lSDL” when it
should naturally just be “gcc foobar -lSDL”. This is confusing for new
users as many do not know the internals of gcc’s expansion of the
commandline to something similar to "ld crtX.o foobar.o (user libs) (gcc
libs)"
In a normal case, crtX calls ‘main’, which if not found, mingw32 calls
WinMain.
In SDL’s case, crtX calls ‘main’, which is found in mingw32, which calls
WinMain. In case WinMain is defined in foobar.c, I believe that
SDLmain’s WinMain is not called. -
In the case SDL is wrapped in a user library, then SDLMain is not
called unless foobar.c has an "include <SDL.h>"
e.g.
- foolib.a is wrapper library that suppresses the need for final
application to require SDL headers to be installed(*). foobar.c will not
have “include <SDL.h>”, but rather “include <foolib.h>”, ‘main’ will not
be redefined, linking will be “gcc foobar.c -lfoolib” and of course
’sdlmain’ will not be called.
-
If linking to SDL and another library (libfoo.a) that redefines
’main’ (the latter would receive the same criticism from my behalf,
btw), then which ‘main’ will be linked? Either ‘sdlmain’, or
’libfoomain’. It will depend on which header is specified first. -
SDL_Init is used for initialization. Why then hijack ‘main’ if we
require SDL_Init to be first called anyway? -
If hijacking ‘main’ is required (maybe BeOS), then do it. But in case
where it brings nothing special (in current svn SDL 1.3 it often just
does commandline arg parsing!), then remove it completely!
(*) In the case of a foolib DLL, even the SDL libraries are not required
during linking since there are no unresolved functions permitted in a
DLL. Note: SDL.dll might be required to run (but not link) application.