I don't understand what this error means

This is a discussion on I don't understand what this error means within the C++ Programming forums, part of the General Programming Boards category; lesson01.cpp .text+0x27): undefined reference to `SDL_Init' lesson01.cpp .text+0x4b): undefined reference to `SDL_SetVideoMode' lesson01.cpp .text+0x62): undefined reference to `SDL_RWFromFile' lesson01.cpp .text+0x72): ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Exclamation I don't understand what this error means

    lesson01.cpp.text+0x27): undefined reference to `SDL_Init'
    lesson01.cpp.text+0x4b): undefined reference to `SDL_SetVideoMode'
    lesson01.cpp.text+0x62): undefined reference to `SDL_RWFromFile'
    lesson01.cpp.text+0x72): undefined reference to `SDL_LoadBMP_RW'
    lesson01.cpp.text+0x97): undefined reference to `SDL_UpperBlit'
    lesson01.cpp.text+0xa2): undefined reference to `SDL_Flip'
    lesson01.cpp.text+0xae): undefined reference to `SDL_Delay'
    lesson01.cpp.text+0xb9): undefined reference to `SDL_FreeSurface'
    lesson01.cpp.text+0xbe): undefined reference to `SDL_Quit'
    collect2: ld returned 1 exit status

    I got this code from a website causeI wanted to experiment with Sdl programing with c++ but even on the tutorial codes and my own I get this kind of error. i was just wondering if it the errors in the code or if its because of some file i dont have. any ways here the code.

    {Basically what is the compiler telling me whats wronge with the commands flip, delay quit}


    /*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not

    be redestributed without written permission.*/


    Code:
    //Include SDL functions and datatypes
    
    #include "SDL/SDL.h"
    
    
    
    int main( int argc, char* args[] )
    
    {
    
        // The images
    
        SDL_Surface* hello = NULL;
    
        SDL_Surface* screen = NULL;
    
    
    
        // Start SDL
    
        SDL_Init( SDL_INIT_EVERYTHING );
    
    
    
        // Set up screen
    
        screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    
    
    
        //Load image
    
        hello = SDL_LoadBMP( "hello.bmp" );
    
    
    
        //Apply image to screen
    
        SDL_BlitSurface( hello, NULL, screen, NULL );
    
    
    
        //Update Screen
    
        SDL_Flip( screen );
    
    
    
        //Pause
    
        SDL_Delay( 2000 );
    
    
    
        //Free the loaded image
    
        SDL_FreeSurface( hello );
    
    
    
        //Quit SDL
    
        SDL_Quit();
    
    
    
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    It seems that the linker cant find the code for the given headers. You never said what compiler or IDE your using so we cant help further. Whatever you are using, you need to somehow tell it what library your using so it can link to it (i.e. SDL). If your using a command line tool then, such as "g++" then you might be able to pass an argument like "-lSDL" or "-lsdl" (the first letter is a lower case "L", not an i or number 1).

    If your following an intro tutorial on SDL, it probably has instructions on how to compile/link your program. Did you follow those?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You have to give a linking instruction to the compiler to tell it that the SDL library has to be linked into the program. If you're compiling this in a console window, add "-lsdl -lsdl_image" to your command. Depending on your setup, you might also have to tell the compiler where to find the SDL headers, using -I[path-to-header].

    For example, in Linux your command might be:
    Code:
    g++ -I/usr/include/SDL -lSDL -lSDL_image  lesson01.cpp -o lesson01
    If you're using an IDE, you'll have to find where to enter the library information and include path for the project, probably in project properties, build options, etc.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Im using G++ a Linux compiler and runing it through the console

    and i just copied this code line for line. how would i go about telling the compiler about the library.

    I thought i did that when i put #include "SDL/SDL.h"

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The header file is just used really to "define" stuff, like to explain what functions are available, what arguments they accept, etc. (the function's "prototype"). You can find and open the ".h" file yourself and youll see theres probably not too much actual "code", besides definitions.

    Just the "header" file is fine to compile your code. However, to actually make use of the code you have to link in the actual implementations of the functions defined in the header, because right now the functions dont "do" anything. This next step is linking, which is what we both described above. In particular, we both gave examples of how to do it using your exact compiler/linker. Just pass the arguments seen as in R.Stiltskin's nice example.

    EDIT: For starters, just try adding the "-lsdl" argument, just before your input .cpp file. If it complains it doesnt know what/where that is, then manually find and specify the location as an argument, again, as in the above example.
    Last edited by nadroj; 12-12-2009 at 11:25 PM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    ok i did exactly what you said it worked thanks guys. When you

    type this in their it finds the sdl libraries that it was complaining about?

    g++ -I/usr/include/SDL -lSDL -lSDL_image lesson01.cpp -o lesson01

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well initially the problem seemed to be that it didnt know anything about the SDL libraries, so you got the "undefined reference" errors. You then add the "-l<library name>" like "-lsdl" or whatever other libraries the code/headers need. If you still get problems its because either you need more/different libraries to link against or, just as likely I imagine, the linker doesnt know where the libraries are. These are usually ".o" or ".so" binary files on your computer. Specifying the path to the location, as he suggested above with "-l<path>" tells the linker where to find these binary files, and... happy ending.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    thanks guys i will keep this in mind as i study more c++ on my free time thanks I'll only post if im absolutely stuck but i think this experience will help me alot.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21