使用SDL库开发,提示error LNK2001:无法解析的外部符号_main

在使用SDL2库进行开发的时候,一开始会遇到标题所示的问题。

解决办法是自定义SDL_MAIN_HANDLED宏,如#define SDL_MAIN_HANDLED即可解决问题。

产生这个问题的原因是SDL把main函数变成了自己的SDL_main,详见SDL_main.h里,

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

SDL_MAIN_NEEDED和SDL_MAIN_AVAILABLE默认是会开启其中一个的。详见SDL_main.h里,

#ifndef SDL_MAIN_HANDLED
#if defined(__WIN32__)
/* On Windows SDL provides WinMain(), which parses the command line and passes
   the arguments to your main function.


   If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
 */
#define SDL_MAIN_AVAILABLE


#elif defined(__WINRT__)
/* On WinRT, SDL provides a main function that initializes CoreApplication,
   creating an instance of IFrameworkView in the process.


   Please note that #include'ing SDL_main.h is not enough to get a main()
   function working.  In non-XAML apps, the file,
   src/main/winrt/SDL_WinRT_main_NonXAML.cpp, or a copy of it, must be compiled
   into the app itself.  In XAML apps, the function, SDL_WinRTRunApp must be
   called, with a pointer to the Direct3D-hosted XAML control passed in.
*/
#define SDL_MAIN_NEEDED


#elif defined(__IPHONEOS__)
/* On iOS SDL provides a main function that creates an application delegate
   and starts the iOS application run loop.


   See src/video/uikit/SDL_uikitappdelegate.m for more details.
 */
#define SDL_MAIN_NEEDED


#elif defined(__ANDROID__)
/* On Android SDL provides a Java class in SDLActivity.java that is the
   main activity entry point.


   See README-android.md for more details on extending that class.
 */
#define SDL_MAIN_NEEDED


#elif defined(__NACL__)
/* On NACL we use ppapi_simple to set up the application helper code,
   then wait for the first PSE_INSTANCE_DIDCHANGEVIEW event before 
   starting the user main function.
   All user code is run in a separate thread by ppapi_simple, thus 
   allowing for blocking io to take place via nacl_io
*/
#define SDL_MAIN_NEEDED


#endif
#endif /* SDL_MAIN_HANDLED */

 

如果想要让这两个宏都不定义,那么最好的办法就是自己定义一个SDL_MAIN_HANDLED的宏,

意思就是自己有main函数,不需要sdl提供。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个错误通常表示在使用SDL时,编译器找不到main函数的定义。根据引用和引用,可能有两种原因导致这个错误。 第一种原因是缺少main函数的定义。在C++程序中,main函数是程序的入口函数,如果没有定义main函数,编译器就无法找到程序的入口。解决这个问题的方法是在代码中添加main函数的定义。 第二种原因是SDL重定义了main函数。根据引用,解决这个问题的方法是在引入SDL之前,使用#undef main取消SDLmain函数的重新定义。然后,可以在代码中定义main函数。 综上所述,要解决这个错误,你可以尝试以下两种方法: 1. 确保你的代码中有main函数的定义。 2. 如果你使用SDL,并且遇到了SDL重定义main函数的问题,可以在引入SDL之前使用#undef main来取消SDLmain函数的重新定义。然后,在代码中定义main函数。 希望这个解答对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [MSVCRTD.lib(exe_main.obj) : error LNK2019: 无法解析外部符号 _main,该符号在函数 "int __cdecl ...](https://blog.csdn.net/QIUCHUNHUIGE/article/details/78337915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [提示出现unresolved external symbol _main的解决方法](https://download.csdn.net/download/weixin_38628429/14000775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值