user3325226
user3325226

Reputation: 339

C++ SDL2 - Incomprehensible link error

I setup SDL2 under C++ CDT Eclipse. I added the include path "........SDL2-2.0.3\i686-w64-mingw32\include", the library path "........SDL2-2.0.3\i686-w64-mingw32\lib" and added the libraries 1."mingw32" 2."SDL2main" 3."SDL2". So now, if I add a main.cpp with this content:

#include <SDL.h>

int main(int argc, char* args[])
{

    return 0;
}

I can build the project fine, but if I use this:

#include <SDL.h>

int main()
{

    return 0;
}

The project can't build and I get this error:

Info: Internal Builder is used for build g++ "-IO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\include\SDL2" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\main.cpp" g++ "-LO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib" -o "SDL OpenGL Lab.exe" main.o -lmingw32 -lSDL2main -lSDL2 O:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib/libSDL2main.a(SDL_windows_main.o): In function console_main': /Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference toSDL_main' collect2.exe: error: ld returned 1 exit status

I simply wonder me why this error is depending on my main method, I want to use the main method I want. Why this is so, how I can fix this ?

Upvotes: 0

Views: 2840

Answers (2)

Le&#239;ly Coquard
Le&#239;ly Coquard

Reputation: 19

I had the same problem and had to go through a lot of manipulations. What I have found is the simple line #define SDL_MAIN_HANDLED before calling SDL.h. SDL has his own main that's why, I think. For example, in my .h files, I write everytime this :

#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED 
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

Upvotes: 1

Topological Sort
Topological Sort

Reputation: 2787

Sorry, you're stuck with it. I also preferred a simpler main, but SDL doesn't support it.

Here's a little more on SDL and main in the Windows world. It doesn't say why you need their version of main -- but you do.

I get "Undefined reference to 'SDL_main'" ...

Make sure that you are declaring main() as:

#include "SDL.h"

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

https://wiki.libsdl.org/FAQWindows

Upvotes: 3

Related Questions