Public Group

[SDL] undefined reference to "WinMain@16"

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

Recommended Posts

I'm writing my first SDL program and I get the link error "undefined reference to 'WinMain@16'". Here is my code. Its a pretty simple "hello world" program.
#include <SDL/SDL.h>

int main(int argc, char* argv[]){
//initialize SDL and the video subsystem
if(SDL_Init( SDL_INIT_VIDEO ) < 0)
return -1;

//signal SDL to change the text of the main window to "SDL Hello World"
SDL_WM_SetCaption("Hello World", "Hello World");

//create an SDL_Surface object which represents the game window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, 0);

SDL_Surface* temp = SDL_LoadBMP("hello.bmp");

SDL_Surface* bg = SDL_DisplayFormat(temp);

SDL_FreeSurface(temp);

SDL_Event event;
bool quit = false;

while(!quit)
{
if( SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;

case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
}
break;
}
}

SDL_BlitSurface(bg, NULL, screen, NULL);

SDL_UpdateRect(screen, 0, 0, 0, 0);
}

SDL_FreeSurface(bg);

SDL_Quit();

return 0;
}


I'm using bloodshed's Dev-C++ IDE I put "libSDL", "libSDLmain", and "libgdi32" in my linker. I know I have gottedn this "WinMain@16" error before and thought I had fixed it by linking "libgdi32" but it doesn't fix the error this time. Any help would be really appreciated so I can get into SDL.

Share on other sites
WinMain is the entry point for all win32 applications, which is what you're project is setup for. Either change your main() function to winmain() or change your project settings to compile a console app.

Cheers.

Share on other sites
For common questions you might want to try the forum search.

Share on other sites
That's what you get when you don't link against SDLmain, it may be because you're missing the extension from the libs, remember its either libSDLmain.a or -lSDLmain.

Still, something seems fishy, you should get lots of other undefined references if you were in fact doing it the wrong way for both libs, you should post the linker output, call and all.

Share on other sites
Quote:
 Original post by arudsonWinMain is the entry point for all win32 applications, which is what you're project is setup for. Either change your main() function to winmain() or change your project settings to compile a console app.Cheers.

SDL uses main and provides its own WinMain, otherwise, it wouldn't truly be crossplatform.

Share on other sites

-lmingw32
-lSDLmain
-lSDL

I noticed that it helps if they are in that order.

Share on other sites
Well, I got it to work. I didn't have to change my code at all, it was actually just the order in which I linked the mingw32, SDL and SDLmain libraries (thanks cNoob). Can anyone explain why that makes such a difference though?

Share on other sites
Quote:
 Original post by chaulkyWell, I got it to work. I didn't have to change my code at all, it was actually just the order in which I linked the mingw32, SDL and SDLmain libraries (thanks cNoob). Can anyone explain why that makes such a difference though?

Because if library B uses functions in library A library A must be referenced first in order for B to use the functions defined in A, don't ask me why, but thats the way gcc works.

• 9
• 9
• 13
• 41
• 15