954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[Linker error] undefined reference to `SDL_Init'

At first, here's my source code:

#include <SDL.h>

int main(int argc, char* argv[])
{
 //initialize SDL and the video system.
 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);
 
 //Load the SDL logo bitmap to a temporary surface
 SDL_Surface* temp=SDL_LoadBMP("data\\structures\\sdl_logo.bmp");
 
 //Create the working SDL surface which matches the display format of the temporary surface.
 SDL_Surface* bg= SDL_DisplayFormat(temp);
 
 //Free the memory allocated to the temporary SDL_Surface.
 SDL_FreeSurface(temp);
 
 SDL_Event event;
 bool quit=false;
 
 //this is the main message loop if the game.
 while(!quit)
 {
   //Check message qeue for an event.
   if (SDL_PollEvent(&event))
   {
     //If an event was found.
     switch (event.type)
     {
       //check to see if the window was closed via the "x"
       case SDL_QUIT:
       //Set the quit flag to true
       quit=true;
       break;
       
      //Check the keyboard to see if the ESC key was pressed.
      case SDL_KEYDOWN:
       switch (event.key.keysym.sym)
       {
         case SDLK_ESCAPE:
           //set our quit flag to true
           quit=true;
          break;
       }
      break; 
     }
   } 
   //Draw the background sprite:
   SDL_BlitSurface(bg, NULL, screen, NULL);   
   
   //Update the current window.
   SDL_UpdateRect(screen,0,0,0,0);      
 }

//Free the allocated memery for the background surface.
SDL_FreeSurface(bg);

//Quit SDL and allow it to clean up everything.
SDL_Quit();

//return control to windows with no errors.
return 0;
}

Well... my problem is, that i in some way must have mis-configured, my compiler. But since i'm new (Both at programming c++ and at using the compiler), i don't know how :S
the error meesages are, the following:
[Linker error] undefined reference to `SDL_Init'
[Linker error] undefined reference to `SDL_WM_SetCaption'
[Linker error] undefined reference to `SDL_SetVideoMode'
[Linker error] undefined reference to `SDL_RWFromFile'
[Linker error] undefined reference to `SDL_LoadBMP_RW'
[Linker error] undefined reference to `SDL_DisplayFormat'
[Linker error] undefined reference to `SDL_FreeSurface'
[Linker error] undefined reference to `SDL_PollEvent'
[Linker error] undefined reference to `SDL_UpperBlit'
[Linker error] undefined reference to `SDL_UpdateRect'
[Linker error] undefined reference to `SDL_FreeSurface'
[Linker error] undefined reference to `SDL_Quit'
[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
As you can see, any command that i have used, which starts with SDL, has an undefinded referance. As i have come to understand, it has something to do with the way i have inclueded my files.
If some-one out there have used the dev-c++ compiler (Which i'm using), and also have experiaence with connecting that compiler to the SDL, it would be REALY nice if you would give me a, step, by step, tutorial, in how to do it. But it would also be nice if any1 else would bother to anwser, because that might be enouth to help me.
Please take in notice, that i'm VERY new to the langauge and compiler, so please don't use any words/sentences, that only experienced users understand

MedianHansen
Newbie Poster
18 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

I think the question here is, if anyone can be bothered to download SDL to test it.

Personally, I don't think anyone is going to do that. Btw I have dev-cpp :)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

here is something obtained thru googling (i've no clue about either dev-c++ or sdl); see if these help.
http://www.gpwiki.org/index.php/C:How_to_set_up_your_SDL_Build_Environment#Windows:_Dev-C.2B.2B
http://www.imrtechnology.ngemu.com/sdldevtut.htm
if they do not, abandon daniweb and try a forum where there may be people who are more knowledgable. eg. http://gpwiki.org/forums/viewforum.php?f=2

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Well... ty both, that seemewd to help, but now i'm getting some strange new errors :S Which i don't understand...

multiple definition of `main'
first defined here
.drectve `/DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"MSVCRT" /DEFAULTLIB:"OLDNAMES" ' unrecognized
[Linker error] undefined reference to `_alloca_probe'
[Linker error] undefined reference to `_alloca_probe'
[Linker error] undefined reference to `_alloca_probe' (Yeah, this error comes three times)
ld returned 1 exit status
C:\Dev-Cpp\Projekt hej\Makefile.win [Build Error] [HEJ.exe] Error 1

The two last errors should correct themselves, if the others are corrected (At least thats what i have come to understand).
But i don't understand that it says i have two main functions, cause i don't
and neither do i understand the '_alloca_probe', i haven't even used it in my source code:

#include <SDL.h>

int main(int argc, char* argv[])
{
 //initialize SDL and the video system.
 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);
 
 //Load the SDL logo bitmap to a temporary surface
 SDL_Surface* temp=SDL_LoadBMP("data\\structures\\sdl_logo.bmp");
 
 //Create the working SDL surface which matches the display format of the temporary surface.
 SDL_Surface* bg= SDL_DisplayFormat(temp);
 
 //Free the memory allocated to the temporary SDL_Surface.
 SDL_FreeSurface(temp);
 
 SDL_Event event;
 bool quit=false;
 
 //this is the main message loop if the game.
 while(!quit)
 {
   //Check message qeue for an event.
   if (SDL_PollEvent(&event))
   {
     //If an event was found.
     switch (event.type)
     {
       //check to see if the window was closed via the "x"
       case SDL_QUIT:
       //Set the quit flag to true
       quit=true;
       break;
       
      //Check the keyboard to see if the ESC key was pressed.
      case SDL_KEYDOWN:
       switch (event.key.keysym.sym)
       {
         case SDLK_ESCAPE:
           //set our quit flag to true
           quit=true;
          break;
       }
      break; 
     }
   } 
   //Draw the background sprite:
   SDL_BlitSurface(bg, NULL, screen, NULL);   
   
   //Update the current window.
   SDL_UpdateRect(screen,0,0,0,0);      
 }

//Free the allocated memery for the background surface.
SDL_FreeSurface(bg);

//Quit SDL and allow it to clean up everything.
SDL_Quit();

//return control to windows with no errors.
return 0;
}
MedianHansen
Newbie Poster
18 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

_alloca_probe is probably being called from one of the library functions you call. That happens a lot.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yeah, i kinda thought that too... but how do i correct the error then?

MedianHansen
Newbie Poster
18 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Okay... this is strange, the same errors comes, even if i break the source code down to:

#include <SDL.h>

int main(int argc, char* argv[])
{

}
MedianHansen
Newbie Poster
18 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

try:

#include "SDL/SDL.h"
Sturm
Veteran Poster
1,079 posts since Jan 2007
Reputation Points: 343
Solved Threads: 24
 

add -lmingw32 -lSDLmain -lSDL to the Linker parameter in additional command-line options

(project menu, project options, parameters tab)

lyndsayw
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

add -lmingw32 -lSDLmain -lSDL to the Linker parameter in additional command-line options

(project menu, project options, parameters tab)

Nothing like being 2 years late :(

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Nothing like being 2 years late :(


I realized before I posted that it was old.

It's also the first search result in google when searching for the error.

lyndsayw
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You