SDL2: LNK1561: entry point must be defined

限于喜欢 提交于 2019-12-17 18:33:56

问题


I want to compile this code:

#include <SDL.h>

int main(int argc, char* argv[]) {
    return 0;
}

But it can't be linked: Error 1 error LNK1561: entry point must be defined

There is some strange code in this library: http://hg.libsdl.org/SDL/file/75726efbf679/include/SDL_main.h

#define main    SDL_main

Also I added SDL2.lib;SDL2main.lib to Project Settings => Linker => Input.

What can I do to run this project?
VS 2012 SP3, empty C++ project.


回答1:


According to this thread on Dream.In.Code:

Right click on project name -> Properties -> Expand Linker tab -> System -> SubSystem: make sure that it is Console (/SUBSYSTEM:CONSOLE)

Alternatively, if you want to hide the console window or are debugging a multithreaded application, you should set the SubSystem to Window (/SUBSYSTEM:WINDOW) instead.




回答2:


I have found that setting /SUBSYSTEM:CONSOLE is only half of the solution. The second half is to add SDL_MAIN_HANDLED to your additional defines. The clue I used to resolve this can be found in SDL_main.h. Setting SDL_MAIN_HANDLED tell the SDL header files that you've already provided a main function and do not wish for it to redefine its own entry point.




回答3:


DON'T #undef main! while its a really bad practice on the SDL side to redefine it, they have good reasons: WinMain is defined on the library side and used to run some init code, helping with compatibility issues. (even more when using different SDL implementations, like Steam's or porting to other platforms like Android)

So what should you do? When on Windows, you should always include SDL2main.lib before SDL2.lib and make sure your main is in the format:

int main(int argc, char* argv[]) // CORRECT
void main(int argc, char* argv[]) // WRONG
int main(int, char**) // MAY BE CORRECT

Source: SDL2 Windows FAQ




回答4:


As a tinky_winki wrote

Right click on project name -> Properties -> Expand Linker tab -> System -> SubSystem: make sure that it is Console (/SUBSYSTEM:CONSOLE)

But if you don't expect console with window simply use, /SUBSYSTEM:WINDOWS




回答5:


Project >> Properties >> Linker >> Advanced >> entry point = main and apply



来源:https://stackoverflow.com/questions/18672303/sdl2-lnk1561-entry-point-must-be-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!