18

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.

Dmitry
  • 7,457
  • 12
  • 57
  • 83
  • 2
    possible duplicate of [Why SDL defines main macro?](http://stackoverflow.com/questions/11976084/why-sdl-defines-main-macro) – Mike Seymour Sep 07 '13 at 10:25
  • 2
    @MikeSeymour, it's not a duplicate: that thread doesn't contain an answer. – Dmitry Sep 07 '13 at 10:32
  • @computer, yes, console. – Dmitry Sep 07 '13 at 10:35
  • I just started learning c and encountered the same error, this error means something not right with `main()`, in my code I had it misspelled as `Main` with capital m, after correcting it the error disappeared. – razz Jul 28 '14 at 19:27

6 Answers6

44

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.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Adding that if you can get int main() to compile on its own then do make sure you've checked your compiler settings as mentioned here, as depending on how you've created your project may mean this setting is not done for you already. As was my problem. – ReturnVoid Nov 20 '16 at 06:00
  • 1
    I'm adding to this answer. If you are debugging an SDL2 app, and you've added multithreading to said app, instead of using `Console (/SUBSYSTEM:CONSOLE)`, use `Window (/SUBSYSTEM:WINDOW)`. This should also let Visual Studio 2017 attach its local debugger to your SDL2 app successfully, allowing you to debug multiple threads concurrently. – tom_mai78101 May 12 '18 at 01:53
  • Out of time to edit my last comment... Reason why you must go with `Window (/SUBSYSTEM:WINDOW)` is because `Console (/SUBSYSTEM:CONSOLE)` is single-threaded only. – tom_mai78101 May 12 '18 at 02:03
7

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.

dpiron
  • 99
  • 1
  • 3
  • This is the correct way to solve this problem - good debugging skills @dpiron. If your project is set up correctly to begin with you shouldn't need to change Linker properties at this stage – user3001499 Sep 15 '17 at 17:20
  • To add more information to what @user3001499 is referring to, you need to start with a **win32** application project, see [VisualC.html](http://hg.libsdl.org/SDL/raw-file/0d5da1145cad/VisualC.html). – jrh Sep 25 '18 at 15:25
4

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

Gustavo Maciel
  • 652
  • 7
  • 20
  • The `int main(int, char**)` is correct too. Also, what's about Steam SDL implementation? Google doesn't seem to know about it. – HolyBlackCat Jul 15 '17 at 21:41
  • 1
    @HolyBlackCat https://wiki.libsdl.org/Installation#SteamOS SDL provide a dynamic loader and steam is known to include it's own copy of SDL which is different for SteamOSes – Gustavo Maciel Jul 17 '17 at 00:43
  • Note that it's also necessary to create a **win32** application project. See [VisualC.html](http://hg.libsdl.org/SDL/raw-file/0d5da1145cad/VisualC.html). – jrh Sep 25 '18 at 15:24
1

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

Łukasz Mleczko
  • 173
  • 2
  • 12
0

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

-1

Open sdl_main.h

Change

#define main sdl_main

to

#define sdl_main main

now it will work with simple int main() but this is bad hooray to short term solutions

Sorry IwontTell
  • 466
  • 10
  • 29