Why SDL defines main macro?
Per the SDL Windows FAQ:
You should be using
main()
instead ofWinMain()
even though you are creating a Windows application, because SDL provides a version ofWinMain()
which performs some SDL initialization before calling your main code.If for some reason you need to use
WinMain()
, take a look at the SDL source code insrc/main/win32/SDL_main.c
to see what kind of initialization you need to do in yourWinMain()
function so that SDL works properly.
SDL requires initialization, so it injects its own main
function that runs its initialization before calling your "main" function, which it renames to SDL_main
so that it does not conflict with the actual main
function. As noted in the FAQ, your main
function must be of the form
int main(int argc, char* argv[])
I'm using the SDL functions without the SDL_main be defined. Is that fine?
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 #include
ing 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.
Why we have to pass parameters in main functions while sdl setup in Mingw?
Let me explain.
In usual program you can use one of two valid forms of main
:
int main()
int main(int argc, char **argv) // arguments' names do not matter here
The difference between them is that second form allows your program to take command-line arguments.
But SDL uses a special
main
related hack.SDL has #define main SDL_main
somewhere in the headers.
Because of that, preprocessor will replace int main(...)
with int SDL_main(...)
.
Actual main()
is located somewhere in SDL code. It does some initialization and then calls your "main
" (which is SDL_main
).
Actual main()
may look like this:
extern SDL_main(int, char **);
int main(int argc, char **argv)
{
// SDL initialization
SDL_main(argc, argv);
}
It shows why you can's use int main()
. SDL source has no prototype for int SDL_main()
because C does not support function overloading. There can be only one prototype for SDL_main
and SDL developers decided that it should be int SDL_main(int, char **)
.
The reasons for this are clear: Many applications need to work with command line arguments. And many programmers are used to work with int main(int, char **)
when they want to read a command line arguments.
Undefined reference to 'SDL_main' while using Dev C++
This error is common when using int main()
instead of :
int main(int argc, char **argv)
//or
int main(int argc, char *argv[])
Try replacing it with either of these.
In the background, SDL defines a macro #define main SDL_main
that renames your main(int argc, char *argv[])
function so that it does not conflict with its own main()
function (used for SDL initialization). If you use main()
instead, the macro does not modify it and SDL_main
is then not found.
If it does not work, follow these steps:
When you create your project, make sure you choose a
Win32 GUI
orWin32 Console
application type.After creating your project, I assume you added the following command line to your project parameters under linker :
-lmingw32 - -lSD2main -lSDL2
Then put
SDL2.dll
in your project directory where your executable will be.Include
SDL2.h
beforemain(int argc, char **argv)
begins in your source code.
SDL2: LNK1561: entry point must be defined
According to this thread on Dream.In.Code:
Right click on project name -> Properties -> Expand Linker tab -> System -> SubSystem: make sure that it is
Console (/SUBSYSTEM:CONSOLE)
Alternatively, if you want to hide the console window or are debugging a multithreaded application, you should set the SubSystem to Window (/SUBSYSTEM:WINDOW)
instead.
Linker Error when using the SDL Library: _main already defined in main.obj
Try also this at the top of your main.cpp
file:
#define SDL_MAIN_HANDLED
That is supposed to cause SDL to skip all of its main
nonsense.
Note that it needs to happen before you include SDL
:
#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"
Undefined reference to 'SDL_main'
The only plausible reason for your problem I can think of is that when you created the file with main
in it, you forgot to add it to build targets.
You should see CApp.cpp in the list where my main.cpp is. Right click on it and click Properties. Click on Build tab in the window that pops up. You should see this:
Click OK, hit Ctrl+F11 (Rebuild).
Good luck.
Related Topics
Std::Endl Is of Unknown Type When Overloading Operator≪≪
Is Ncurses Available For Windows
How to Include Libraries in Visual Studio 2012
Elegant Solution to Duplicate, Const and Non-Const, Getters
Removing Item from Vector, While in C++11 Range 'For' Loop
Why Do We Need a Pure Virtual Destructor in C++
Vector Going Out of Bounds Without Giving Error
Why Doesn't Delete Set the Pointer to Null
Dealing With Accuracy Problems in Floating-Point Numbers
When Can Outer Braces Be Omitted in an Initializer List
Tellg() Function Give Wrong Size of File
How to Convert a Lambda to an Std::Function Using Templates
Correct Way of Declaring Pointer Variables in C/C++
Differences Between Unique_Ptr and Shared_Ptr
What Is the Meaning of the Term "Free Function" in C++
Difference Between Conversion Specifiers %I and %D in Formatted Io Functions (*Printf/*Scanf)
How to Find Which Elements Are in the Bag, Using Knapsack Algorithm [And Not Only the Bag'S Value]