Jump to content
  • Advertisement
Sign in to follow this  
chaulky

[SDL] undefined reference to "WinMain@16"

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
In your linker box link with these files:

-lmingw32
-lSDLmain
-lSDL

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!