[SDL] undefined reference to "WinMain@16"

Started by
6 comments, last by Kwizatz 17 years, 2 months ago
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.
~ Chaulky
Advertisement
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.
For common questions you might want to try the forum search.
Ra
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.
Quote:Original post by arudson
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.


SDL uses main and provides its own WinMain, otherwise, it wouldn't truly be crossplatform.
In your linker box link with these files:

-lmingw32
-lSDLmain
-lSDL

I noticed that it helps if they are in that order.
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?
~ Chaulky
Quote:Original post by chaulky
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?


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.

This topic is closed to new replies.

Advertisement