I think you want
#define SDL_MAIN_HANDLED
in your main file, BEFORE the line
#include <SDL2/SDL.h>
Explanation:
In SDL / SDL2, in an effort to try to make cross-platform development of certain kinds of applications simpler, SDL creates a custom "entry-point" to your application. That is, your int main()
is not the real main. What happens is, main
is defined as a macro in the SDL header and this causes your main to be renamed to SDL_main
or similar. Then, in the "SDL_main" library a different main
is defined which will be the real main
of your application. This main
just fetches the command-line arguments in whatever way is appropriate for the platform, and calls your main
(which was renamed SDL_main
).
On windows, there are also some differences regarding whether your application is started as a console program vs. as a gui program, iiuc.
Sometimes you want SDL to do these things for you, but if you are developing a traditional console program, usually you don't. So you pass SDL this SDL_MAIN_HANDLED
define in order to prevent from doing all this stuff.
The #undef main
approach will work also, but it's not quite as good because this way, you tell SDL what is going on, with the other method, SDL thinks that all it's stuff is going to be used and in fact you crudely disable it with #undef
later.
If you want to see details of the various macros / platform checks, you can look in the SDL_main.h
header. If you want to know what the benefits of the SDL main system are, you can look at SDL documentation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…