Shambhav
Shambhav

Reputation: 853

What is the purpose of libSDL2main.a?

Some sources say to have -lSDL2main.a as one of the files to link against while building SDL2 programs but some don't. The program works fine in either cases. What is it for?

Upvotes: 4

Views: 1613

Answers (1)

keltar
keltar

Reputation: 18399

Its purpose is to make entry point the same on all platforms - int main(int argc, char **argv), with UTF8-encoded arguments. While it is so on e.g. linux (where SDLmain doesn't exist or empty), other target systems may follow different scheme.

For example, windows programs may have different entry points - main, WinMain, wmain. With SDLmain, you don't need to handle it differently - just use main like on other systems (renamed by SDL to SDL_main).

Basically on your code, it only boils down to this line in SDL_main.h header (included automatically if you inclue SDL.h):

#define main    SDL_main

(only on systems that have implementation for SDLmain). It renames your main to SDL_main and injects system-specific entry point (implemented in SDL2main library), which converts arguments, does some platform-specific initialisation, and calls your SDL_main.

Whether that is a good thing is debatable; this functionality could be disabled with #define SDL_MAIN_HANDLED before including SDL.h. In that case, you define your own platform-specific entry point, do all required initialisation, but SDL asks you to call SDL_SetMainReady before calling any other SDL stuff.

So, to answer your question why sometimes it is used and othertimes it doesn't - it is so because in some cases when you already have "standard" main it does nothing.

Upvotes: 6

Related Questions