Author Topic: About SDL_main in Main.cpp.  (Read 2046 times)


  • Moderator
  • Freak
  • *
  • Posts: 759
  • Karma: 25
    • View Profile
About SDL_main in Main.cpp.
« on: 2006-12-14 10:06:59 »
Just like main() is the entry point for C programs (inc. C++, Objective-C, and Objective-C++), SDL_main() is the main entry point for SDL programs. However, you don't actually write an SDL_main() function. The header file "SDL_main.h" remaps your main() function to the SDL_main() function with a function macro. Your SDL_main() function is called after the code in SDLMain.m has performed the required "bootstrap" initializations to support the SDL runtime.

There are three things you have to do:
You must include either SDLMain.m/.h or libSDLmain in your application, because this is the code that defines SDL's entry point. If you fail to do this, it is likely that "_main undefined" will be thrown by the linker.
You must give your main() procedure the following prototype:

int main(int argc, char*argv[]);
You must make sure the file containing your main() procedure #includes SDL.h.
Otherwise, the macro will not remap main() to SDL_main(), you will get an undefined _main error, or the bootstrap process will not run, and SDL will behave strangely or your application will crash or hang.

Just include SDL header and remove unnesesary if else construction. (I don't remember who add this =))

Code: [Select]
#ifdef _MSC_VER
extern "C" int
SDL_main(int argc, char *argv[])
main(int argc, char *argv[])


  • Crazy poster
  • *
  • Posts: 105
  • Karma: 0
    • View Profile
    • gigaherz's shitty stuff
Re: About SDL_main in Main.cpp.
« Reply #1 on: 2006-12-14 20:10:59 »
I did that... I didn't know about SDL_main.h, and the code worked fine without anything else in the other compilers, so instead of risking it to break the other compilers, I #ifdef'ed it. If including SDL_main.h makes it works without the #ifdef, then I will fix it.