开发者

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup, but this time it's NOT a Windows/Console problem!

开发者 https://www.devze.com 2023-03-23 02:18 出处:网络
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).

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,开发者_StackOverflow中文版 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;
}


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


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;


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消