Skip to main content
gamedev.net
🔒 Locked

I need help setting up SDL with Code::Blocks please.

Started by PowerPython Mar 29, 2007 at 11:55 AM 9 replies 2.1k views
Original Post
PowerPython
PowerPython
OK, so I went through everything on this tutorial: http://lazyfoo.net/SDL_tutorials/lesson01/windows/codeblocks/index.php However, when I tried to run the program, I got a lot of messages in red font saying something like (obviously I am not going to write them all out!): SDL_INIT_ undeclared (use this function first) I am using the most recent SDL version and I followed every instruction on tha tutorial! I am using the latest code::blocks, I am trying to write a c++ program by the way. Any help? Has anyone had this problem before? Any help appreciated! Thanks, Sophie.
Kylotan
Kylotan
Hi Sophie,

We could really do with seeing the exact text of the first one or two errors. It sounds like you haven't done the "#include " at the top of your source file, but it's hard to be sure.
PowerPython
PowerPython
I am using the code automatically generated by Code::Blocks for and SDL app, it has the headers at the top.

OK, I just went to open the project and run it and now it says:

ld.exe cannot find -l-lmingw32

What is this about?



Sophie
PowerPython
PowerPython
I took away the -l from the library path and got this:


--------------------------------------------------------------------------------
Switching to target: default
Linking executable: sdlapp.exe
mingw32-g++.exe: D:\Documents and Settings\Sophie\Desktop\SDL-1.2.11\lib\libSDL.dll.a: No such file or directory
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings


This is confusing, or maybe I am just dumb Laughing!



OK, I am dumb! I just got what that error message means, so I went through and found that file under MinGW32\lib. Then I recompiled it and got a new message, this one I can't decipher!:

\home\hercules..... 217 undefined refernece to 'SDL_main'


Did I do something wrong by changing the last error?


Sophie
Kylotan
Kylotan
The -l bit is the stuff that tells the compiler to look for a certain library; unfortunately some IDEs require you to write it explicitly, whereas some don't, like Code::Blocks.

I don't know quite how you fixed the last error so it's hard to say if you did it the best way or not! But it may well be ok.

What you have now is an undefined reference to another function, which you have to link in from elsewhere, namely SDL_main.lib if I recall correctly. That needs to be added to the list of additional libraries, which will probably already have 'sdl.lib', or maybe 'libsdl', or something similar in there.
PowerPython
PowerPython
Where do I look to check that?


The previous one I had moved the DLL location so needed to change the path names
Kylotan
Kylotan
It'll be in the project settings somewhere, perhaps under 'Linking' or 'Libraries' or something of a similar name. I don't have Code::Blocks here so I can't check the exact place for you.
Servant of the Lord
Servant of the Lord
It sounds to me like he didn't set up int main() the SDL way. SDL changes main, by way of a macro, to SDL_main, but it sounds like PowerPython didn't give it the correct parameters, so he is trying to access 'int SDL_main()' which is undefined, when what he really wants is 'int SDL_main(int, char**)'. I'm not familiar with Code::Blocks, but this is my guess.

@PowerPython: change your main() entry to the following and recompile.
     int main(int argn, char *argc[])


[Edited by - Servant of the Lord on March 30, 2007 1:30:14 PM]
PowerPython
PowerPython
I have all of that, here is my code, it is the Code::Blocks example that was automatically generated:

#ifdef __cplusplus    #include <cstdlib>#else    #include <stdlib.h>#endif#include <SDL/SDL.h>int main ( int argc, char** argv ){    // initialize SDL video    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )    {        printf( "Unable to init SDL: %s\n", SDL_GetError() );        return 1;    }    // make sure SDL cleans up before exit    atexit(SDL_Quit);    // create a new window    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,                                           SDL_HWSURFACE|SDL_DOUBLEBUF);    if ( !screen )    {        printf("Unable to set 640x480 video: %s\n", SDL_GetError());        return 1;    }    // load an image    SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");    if (!bmp)    {        printf("Unable to load bitmap: %s\n", SDL_GetError());        return 1;    }        // center the bitmap on screen    SDL_Rect dstrect;    dstrect.x = (screen->w - bmp->w) / 2;    dstrect.y = (screen->h - bmp->h) / 2;    // program main loop    bool done = false;    while (!done)    {        // message processing loop        SDL_Event event;        while (SDL_PollEvent(&event))        {            // check for messages            switch (event.type)            {                // exit if the window is closed            case SDL_QUIT:                done = true;                break;                // check for keypresses            case SDL_KEYDOWN:                {                    // exit if ESCAPE is pressed                    if (event.key.keysym.sym == SDLK_ESCAPE)                        done = true;                    break;                }            } // end switch        } // end of message processing        // DRAWING STARTS HERE                // clear screen        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));        // draw bitmap        SDL_BlitSurface(bmp, 0, screen, &dstrect);        // DRAWING ENDS HERE        // finally, update the screen :)        SDL_Flip(screen);    } // end main loop    // free loaded bitmap    SDL_FreeSurface(bmp);    // all is well ;)    printf("Exited cleanly\n");    return 0;}
Kylotan
Kylotan
Did you add the SDL_main library? You need to bring up the Project properties, then Build Options, select the Linker tab, and then click Add to navigate to that library.
PowerPython
PowerPython
I got it working! Know idea how, I just uninstalled everything (including code::blocks) and it just worked!


Thanks for trying to help me anyway,

Sophie

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.