[SDL] sdlmain
Julien Lecomte
julien at famille-lecomte.net
Tue Jul 4 08:24:03 PDT 2006
On 04/07/2006 15:12, Torsten Giebl wrote:
>> 1) 'main' is standard C function whose prototype is exactly:
>> int main(int, char**, char**).
>
> What would be this second char** ?
envp: pointer to environment block.
It's the original environment variables that were in use during launch
of process.
Not all implementations return it, and very few people use it, that's
why we usually never see it. Implementations that don't return envp
return a null pointer instead to avoid a segfault if trying to access it.
mingw, msys and my linux-box (a gnu-linux LFS) return envp.
e.g:
----
#include <stdio.h>
int main(int argc, char* argv[], char* envp[])
{
int i = 0;
if (!envp)
printf("your implementation doesn't return envp)\n");
else
while (envp[i])
{ printf("%2d) %s\n", i, envp[i]); i++; }
return 0;
}
----
> The good thing about SDLmain is you can use it.
> But you are not forced to use it. For me years ago
If you can opt-in or opt-out SDLmain, and using SDLmain brings nothing;
then why have it?
> as a C/C++ beginner it was better and easier just
> to use int main ...... then using int WinMain ....
And more portable ;-)
The runtime library should be in charge not a user library.
> I do not know what BeOS needs.
Neither do I, that's why I put the conditional.
> That way i can write code that "just works" on all
> the platforms and i do not have to think about
> what main definition i need on what platform.
Having a prototype in SDL_main.h denies portability since compiler will
usually check prototype and declaration matches and either throw a
warning in best of cases and a error in worst.
The C linker does not do type checking.
Anyway, if SDL_main must stay or not, it should be fixed in current
implementation:
- not define a prototype (line 52 of SDL_main.h)
- call SDL_main with (argc, argv, 0) if SDL can't provide envp to avoid
a segfault, or with a envp if it can.
I'll point out that this fix also "breaks" a 'main' in a C++ file; since
'main' is renamed 'SDL_main' the compiler will not think of it as
needing C linkage thus no implicit "extern C".
C++ users will have to declare 'main' explicitly as "extern "C"
main(...)"; which is counter-intuitive but implementation correct, and
also why I strongly encourage removing SDL_main altogether.
The last point is mentioned in SDL_main.h (line 41), but who reads
header files apart from insomniacs?
There is solution to avoid this; but I'll not even mention it since it's
even more messy!
More information about the SDL
mailing list