6

that's my code:

Lib.h

#ifdef ExportLib
    #define Lib __declspec(dllexport)
#else
    #define Lib __declspec(dllimport)
#endif
extern void Lib Launch();

Lib.cpp

#include <SDL/SDL.h>
#include "Lib.h"
void Launch() {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* win = SDL_CreateWindow("Untitle", 100, 100, 400, 400, 0);
    SDL_DestroyWindow(win);
    SDL_Quit();
}

I build this code to a static library. Then I created a new source file and used this library.

main.cpp

#include "Lib.h"

int main() {
    Launch();
    return 0;
}

Finally, I compile main.cpp using my static library without the SDL_main be defined and the SDL's dependecies. That works fine, the window appears.

But is really fine do it? What functionalities I lost doing it?

genpfault
  • 51,148
  • 11
  • 85
  • 139

2 Answers2

11

SDL_main is for SDL's automatic initialization and cleanup. It's mostly so you don't need to do it manually, though it also goes through the effort of properly setting everything up for a windowed application on the platform where it's compiled, but it's fine to #define the macro SDL_MAIN_HANDLED before #includeing SDL.h, which will prevent SDL from turning main into a macro for SDL_main Simply make sure to initialize and quit SDL properly inside your own code.

If you want to be sure you're doing the necessary initialization right, you can just check the source code and emulate what's there.

Edit:

On some platforms, SDL_Init will fail if you don't use SDL_main. You can disable this failure by calling SDL_SetMainReady before SDL_Init, but be aware this will disable SDL's error handling, and if you improperly initialize SDL after calling SDL_SetMainReady you won't get the clearest of error messages.

Quitting SDL is much more straightforward (and also needs to be done if you're not using SDL_main):

Just call SDL_Quit when you're done with SDL. This will properly close any SDL subsystems presently active.

Community
  • 1
  • 1
jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
  • I saw that the entry point is WinMain and then SDL_main is called. I'm confused with that. How could SDL be compiled to a library if the SDL_main wasn't defined? I have tried to emulate this system but the compiler get me a undefined reference to WinMain. – Mateus Sarmento Dec 04 '15 at 21:58
  • I'm not sure how they set up the project so it compiles and links, though one can probably figure that out by digging through a source download. I did however go looking, and according to [this thread](http://forums.libsdl.org/viewtopic.php?t=9151&postdays=0&postorder=asc&start=0&sid=3043bc6931e9d3732ced6b33516a6286) on the SDL forums (SDL Development>SDL_main, Jun 10, 2013) you may define main *anywhere* in a C program: It just has to exist somewhere, not necessarily in the part of the binary you would think. – jaggedSpire Dec 04 '15 at 22:37
  • Too bad they don't say _which_ operating systems fail. – vesperto Mar 20 '23 at 10:53
1

I'm using the SDL functions without the SDL_main be defined. Is that fine?

Possibly, possibly not. Use SDL_SetMainReady() instead to be sure:

SDL_SetMainReady: Use this function to circumvent failure of SDL_Init() when not using SDL_main() as an entry point.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 3
    After rooting around the docs to figure out how I missed SDL_SetMainReady, I found this comment on the [Initialization Category page](https://wiki.libsdl.org/CategoryInit): "It should be noted that on some operating systems, SDL_Init() will fail if SDL_main() has not been defined as the entry point for the program. Calling SDL_SetMainReady() prior to SDL_Init() will circumvent this failure condition, however, users should be careful when calling SDL_SetMainReady() as improper initialization may cause crashes and hard to diagnose problems." so it looks like its use disables error handling... – jaggedSpire Dec 04 '15 at 17:02