views:

1104

answers:

4

I'm trying to use SDL in C++ with Visual Studio 2008 Express. The following program compiles but does not link:

#include <SDL.h>

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

The link error is:

LINK : fatal error LNK1561: entry point must be defined

I get this regardless of how or if I link with SDL.lib and SDLmain.lib. Defining main as main() or SDL_main() gives the same error, with or without extern "C".

Edit: I solved this by not including SDL.h in main.cpp - a refactoring I did independent of the problem. A similar solution would be to #undef main right before defining the function.

A: 

The linker can't find the entry point. Which means your main() function is not recognized as the entry point.

If you have a .def file, remove it.

Also, if you've set up your project to compile with unicode and not as mbcs, you have to use wmain() instead of main().

Stefan
+2  A: 

I don't have VC++ available at the moment, but I have seen this issue several times.

You need to create a Win32 project as opposed to a console project. A Win32 project expects a WinMain function as a program entry point. SDLmain.lib contains this entry point and the SDL_main.h header file has a macro that remaps your main function to SDL_main. This function is called by the entry point in the SDLmain library.

The main function must have the following signature:

int main(int argc, char *argv[])

It is also required to include SDL.h before the declaration of your main function, and you need to link to both SDL.lib and SDLmain.lib.

It looks like you are doing this. So, my guess is that you have a console project setup. Therefore, the linker is looking for a main function to call, but it is getting remapped to SDL_main by the macro SDL_main.h. So, the linker can't find an entry point and gives up!

Judge Maygarden
Yes, changing from to a windows application (through Project Properties / Configuration Properties / Linker / System / SubSystem) works. Thanks. (I ended up with a different solution, though)
Johannes Hoff
A: 

To me it helped to add the following lines before main():

#ifdef _WIN32
#undef main
#endif

German Wikipedia also suggests to add these lines instead:

#ifdef _WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

Though I still had link errors when I tried second solution.

Delynx
A: 

#ifdef _WIN32 #undef main #endif

Thank you, I have had this problem for days and this is the first solution that has worked. Thank you very much.

Steven