38

So, the infamous error is back. The project is complaining that it can't find the main() method (that's what the error means, right).

However I do have a main, and my project is a Console project, as it should be. It worked before, so I know it's not that.

Also, the project has too many classes and files for me to post them all, so I will post any classes you need by request.

It's a C++, OpenGL and SDL game on Visual Studio 2010. It's not a problem of any of the libraries, as it was working fine before it suddenly and inexplicably showed this linker error.

EDIT: The main() method:

int main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_ALPHA);
 glutCreateWindow("Game");

 glEnable(GL_DEPTH_TEST);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_BLEND);
 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

 g = Game();
 glutInitWindowSize(g.getScreenWidth(), g.getScreenHeight());
 //glutPositionWindow(1280, 50);

 // Callbacks
 glutDisplayFunc(handleRedraw);
 glutReshapeFunc(handleResize);
 glutMouseFunc(handleMouseClicks);
 glutPassiveMotionFunc(handleMouseOvers);
 glutKeyboardFunc(handleKeyboardEvents);
 glutTimerFunc(50, moveItemToInventory, 0);

 glutMainLoop();

 return 0;
}
OddCore
  • 1,534
  • 6
  • 19
  • 32

4 Answers4

96

SDL_main.h is included automatically from SDL.h, so you always get the nasty #define.

Just write:

#include <SDL.h>
#undef main

And it should work fine

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • I put this those code above my classes I did that and got more errors: Error 1 error LNK2005: _main already defined in Main.obj Error 2 error LNK2019: unresolved external symbol "public: void __thiscall Simulation::simulate(void)" (?simulate@Simulation@@QAEXXZ) referenced in function _main Error 3 error LNK2019: unresolved external symbol _SDL_main referenced in function _main All of the help on here didnt work and i tried everything else and it didnt work... please help – Photonic Jan 21 '14 at 21:09
  • 1
    Thanks, worked for me! (and that IS one wierd #define) – Viktor Sehr Jun 10 '15 at 08:33
  • 10
    @ViktorSehr: The rationale is that you can use the same code (`int main() {...}`) in a normal system (Linux, MacOS...) and in Windows. In Windows, to get a program without the console window you have to write `WinMain()` instead of `main()` so SDL replaces your `main()` with `SDL_main()` with that nasty macro and then provides a `WinMain()` in a static library that calls your `SDL_main()` and it just works... except when it doesn't. – rodrigo Jun 10 '15 at 09:31
  • 2
    It seems like this probably isn't the way you should do it anymore (if it was once the proper way), given [this answer](https://stackoverflow.com/a/50087608/574531). – Herohtar Nov 16 '18 at 21:18
  • SDL prefers for your main function to have the form of `int main(int argc, char* argv[])`. This [answer](https://stackoverflow.com/a/11976179/13341069) gives a more detail explanation of why. – Vargo Mar 29 '21 at 13:45
46

Another option would actually to define your own main with the usual parameters

int main(int argc, char *args[])
{
    // Your code here
}

That should get rid of the error.

Then if you don't use those parameters and you also want to get rid of the compiler warning you could do that trick in your main function.

(void)argc;
(void)args;
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
  • This is the real solution. I had `int main(int argc, const char ** argv)` and changing that second param to `char *argv[]` solved the problem. It even says in the comments for the `main` macro in SDL_main.h: "The application's main() function must be called with C linkage, and should be declared like this: int main(int argc, char *argv[])" – daiscog Jan 14 '17 at 23:56
  • @megaflop Interesting, which version of SDL are you using now? I'll have to double check the version I used in the past, I think it was 1.2 on the top of my head. If you can confirm it changed with the recent version I'll edit my post and add this detail! Thanks a lot :) – ForceMagic Jan 16 '17 at 04:49
15

The default solution from SDL documentation:

tl;dr:

#define SDL_MAIN_HANDLED
#include "SDL.h"

full example:

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

#define SDL_MAIN_HANDLED
#include "SDL.h"

int main(int argc, char *argv[])
{
    SDL_SetMainReady();
    SDL_Init(SDL_INIT_VIDEO);

    ...

    SDL_Quit();

    return 0;
}

Source: https://wiki.libsdl.org/SDL_SetMainReady

juwens
  • 3,729
  • 4
  • 31
  • 39
1

The culprit is likely to be SDL_main.h. Check that you don't include that file, there is a nasty define there:

#define main SDL_main
tibur
  • 11,531
  • 2
  • 37
  • 39