Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 946093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:51:37+00:00 2026-05-15T22:51:37+00:00

SDL on OS X uses preprocessor tricks to overload main() with their own entry

  • 0

SDL on OS X uses preprocessor tricks to overload main() with their own entry point, written in Objective C, which calls the user’s main.

These tricks make the lives of non-C SDL users (e.g: the Haskell bindings) very difficult.

Is there a good reason for this?

Why couldn’t SDL do the objective-C Cocoa initialization in SDL_init?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:51:38+00:00Added an answer on May 15, 2026 at 10:51 pm

    The approach for Mac OS X is not much different from the approach for other non-Linux platforms (Windows, old Mac, BeOS). You could ask the SDL developers themselves why they did it this way, but I can see several reasons they may have chosen to do this:

    • This keeps the dependencies of SDL code, which is focused on initializing SDL-specific subsystems (video, audio, timing, etc.) limited to the specific subsystems that SDL is especially designed to work with. I.e. this way, SDL stays lean and mean.
    • It avoids having to introduce a new platform-specific subsystem for application initialization. Not everyone is going to want the bare-bones application object and menu that SDL sets up for Mac apps, not by a long shot – so if you were going to put it into SDL_init, you’d need to make it an optional subsystem so as not to inconvenience developers who don’t need it.
    • It handles inversion of control correctly, which is how Mac OS X and other application frameworks typically operate, while maintaining the operational semantics of SDL routines. SDL_init assumes it’s going to be returning to the caller after initialization is complete, but if you tried naively to create an application object in SDL_init and invoke [app run] on it to finish initializing the application and launching, you’d never return. If you didn’t call run there, you’d have to create a separate SDL function to set up the application run loop. This could complicate the SDL library quite a bit. The approach that was chosen avoids all this, by letting the framework take care of all the application set up first, and invoke the SDL_main() routine from applicationDidFinishLaunching.
    • It makes it easy to convert SDL demos coded on Linux over to Mac OS X. You don’t even have to rename main – the preprocessor renaming of main() to SDL_main() takes care of that for you!

    I’m guessing the last of these reasons is the primary driver behind the redefinition of main in SDL_main.h, which I agree is an ugly hack.

    If you’re prepared to give up that level of cross-platform portability for your library and apps, I’d suggest simply modifying your SDL_main.h to remove the following line:

    #define main SDL_main
    

    and removing the following from the SDLMain.m in your project:

    #ifdef main
    #  undef main
    #endif
    

    You shouldn’t even need to recompile SDL if you do this. Note that SDLMain.m is already set up to invoke SDL_main() without the preprocessor hack, and nothing else in SDL is going to use this, so in this way you can you simply provide SDL_main() as your game’s entry point.

    If you want to go the other way, taking over main() yourself, you’d still want to get rid of the #define main SDL_main hack in SDL_main.h, but other than that, you’re not beholden to the main() that SDL provides for you. First, note that SDLMain.{h,m} are not part of the library proper; you must include them separately in your project. Second, note the following comments in SDLMain.h:

    /*   SDLMain.m - main entry point for our Cocoa-ized SDL app
           Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
           Non-NIB-Code & other changes: Max Horn <max@quendi.de>
    
         Feel free to customize this file to suit your needs
    */
    

    That sounds to me like an invitation to go roll your own if these aren’t working for you, starting with SDLMain.{h,m} as a model. And if you’re rolling your own, you can do what you want! For that matter, you could write the equivalent of SDLMain.m in Haskell, using HOC, if that’s what you want. Unless you’re a whiz with HOC, though, I’d keep it simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing an OpenGL game engine for a while which uses SDL
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I'm working on a c++ code that uses SDL/opengl. Is this possible to create
I'm trying to convert to Java some code that uses SDL. I'm using the
I'm trying to learn to use SDL for a little game I'm writing ,
I've just started experimenting with SDL in C++, and I thought checking for memory
I'm trying to use SDL in C++ with Visual Studio 2008 Express. The following
I am creating a C++ SDL game engine, and it is relevant to know
what is the proper way to scale an SDL Surface? I found one explanation
I'm trying to set up something in SDL [in C++] where I can draw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.