调用SDL时关于main函数被定义,找不到程序入口的问题

在SDL_main.h 中有这样的一段代码

#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
#define main    SDL_main
#endif

其中定义了main函数,所以包含了“SDL_main.h ”之后,不做其他操作的话,编译会报错“LNK1561 必须定义入口点

我查找了网上的资料,分别有以下三种方法:

1.在项目属性->连接器->高级->入口点->填入WinMainCRTStartup

参考链接:https://blog.csdn.net/tmsc123/article/details/71308359
参考链接:https://blog.csdn.net/xhhjin/article/details/6659028

2.在main函数前添加#undef main

在雷霄骅博士的文章中看到,具体文章后来没有找到。。
雷霄骅博士的CSDN主页:https://blog.csdn.net/leixiaohua1020

3.在项目属性->连接器->高级->入口点->填入SDL_main(未能成功

参考链接:https://blog.csdn.net/u013664757/article/details/79762776

实测第一第二种皆可以成功编译
但第三种方法编译后会出现“LNK1221 无法推导出子系统,必须定义它”,按照文章的步骤“修改属性->链接器->系统->子系统 为控制台”后编译出现许多的链接错误。

关于第一种与第二种方法应该都是通过绕过SDL自身的宏定义,关于这点,可以查看下面的文章:
http://www.it1352.com/485754.html

该文章截取部分内容如下:

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.

你可能感兴趣的:(SDL,Visual,Studio)