写本文的目的在于使用网络上的教程及官方的教程(如:http://www.willusher.io/sdl2%20tutorials/2013/08/15/lesson-0-visual-studio)时,编译时都会有错误:
Error LNK2019 unresolved external symbol SDL_main referenced in function main_utf8
如果您要解决这个错误直接看
测试一节。
对于新手来讲,还没有入门就是一个大大的打击。枫竹梦使用的是VS2015 Community版本,操作系统win7 x64。
SDLtest01/
lib/
SDL2.lib
SDL2main.lib
include/
SDL_XXX.h
...
SDL2.dll
main.cpp
5. 在VS中右键项目SDLtest01,选择[属性],在上方配置选择所有配置,因为这样对Debug和Release同时进行了设置,平台选择x64。如果您目标对象为Win32,请选择Win32,注意第4步中也需要使用lib/x86下的lib文件。需要配置的地方有3个,包含目录,lib文件和具体的lib文件。
#include
#include
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}
一般教程都是这样的来配置,生成项目,会提示如下错误:
Error LNK2019 unresolved external symbol SDL_main referenced in function main_utf8
#include
#define SDL_MAIN_HANDLED
#include
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}
int SDL_main(int argc, char *argv[]);
#include
#define SDL_MAIN_HANDLED
#include
int SDL_main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}
/**
* \file SDL_main.h
*
* Redefine main() on some platforms so that it is called by SDL.
*
*/
#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 <===========
*/
/**
* \file SDL_main.h
*
* The application's main() function must be called with C linkage,
* and should be declared like this:
* \code
* #ifdef __cplusplus
* extern "C"
* #endif
* int main(int argc, char *argv[])
* {
* }
* \endcode
*/
#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
#define main SDL_main
#endif
/**
* The prototype for the application's main() function
*/
extern C_LINKAGE int SDL_main(int argc, char *argv[]);
首先,如果是自己定义的main函数,那么需要定义成
int main(int argc, char *argv[]);
而不能使用诸如:
void main();
int main();
...