# undefined reference to SDL_main'

This topic is 2010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using MinGW/Msys and SDL2.
---------------------------------------
These are my console contents.

g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2

D:\SDL2\i686-w64-mingw32\lib/libSDL2main.a(SDL_windows_main.o): In function console_main':
/Users/slouken/release/SDL/SDL2-2.0.2-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference to SDL_main'
collect2.exe: error: ld returned 1 exit status
make: *** [Main.exe] Error 1
[Finished in 0.6s]

--------------------------------------
Here is Main.cpp.

#include <SDL2/SDL.h>

#include <iostream>

int main(int argc, char* argv[]) {
const char* test = "Test";

std::cout << test << std::endl;

return 0;
}


This is my first time using SDL2 instead of SDL 1.2.

Edited by austinsheep

##### Share on other sites

Sounds like you're not linking to the appropriate library.

n!

##### Share on other sites

I think that is a classic ... I have struggled with that error message several times as well, but that was a long time ago.

Maybe those responses help:

http://stackoverflow.com/questions/115813/how-to-statically-compile-an-sdl-game-on-windows

First thing I thought was "isn't -mwindows important as well"!? ... but not sure if that is it.

I also think it is a linking problem.

EDIT:

I think ... (but I am not 100% sure) the last time I fought with this error it was because of a 32-bit vs 64-bit problem. When I used the other libraries it worked as far as I remember ... even if it did not really make sense to me at the time.

Edited by DareDeveloper

##### Share on other sites

Try to compile without -lSDL2main ...

Also, try to swap it with -lSDL2, the order is important when linking.

How did you install SDL2? You may have to link the lib files with their full paths (e.g. -L/PATH/TO/FILE/libSDL2.a )

Edited by dejaime

##### Share on other sites

Try to compile without -lSDL2main ...

Also, try to swap it with -lSDL2, the order is important when linking.

How did you install SDL2? You may have to link the lib files with their full paths (e.g. -L/PATH/TO/FILE/libSDL2.a )

When I compile it without -lSDL2main it returns the error "undefined reference to WinMain@16'"

When I swap it, it returns an even longer error message. (including "undefined reference to SDL_main'")

Didn't I link them correctly? They're on my D: drive.

g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2

##### Share on other sites

I just tried compiling SDL 1.2 and it returned the same error

##### Share on other sites

Try

g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2 -lSDL2main -mwindows

Linkage problems are hard because they aren't obvious and, without seeing all the details, it is hard to know what is and what isn't missing. You can also try sdl-config with --libs or --static-libs as: <sdl-config --libs> without < and >...

Edited by dejaime

##### Share on other sites

SDL uses a macro for main, you have to link the SDL2main lib or "#undef main" after include SDL.h.

Edited by Alundra

##### Share on other sites

Try

g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2 -lSDL2main -mwindows

It returned the same error as to when I switched -lSDL2 and -lSDL2main

SDL uses a macro for main, you have to link the SDL2main lib or "#undef main" after include SDL.h.

When I try statically linking it, I get  "undefined reference to WinMain@16'"

When I added "#undef main" after include SDL.h, I get "undefined reference to 'SDL_main'"

I know it's something I'm doing wrong, I'm just not sure what.  I'm sorry this is is being so complicated.

##### Share on other sites

The problem with linkage is that we can only guess without knowing the details of the system where you are compiling.

Are you trying to link it statically or dynamically?

Try

g++ -o Main.o -lmingw32 -LD:\SDL2\i686-w64-mingw32\lib\libSDL2main.a -LD:\SDL2\i686-w64-mingw32\lib\libSDL2.a -mwindows

Edited by dejaime

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26