# Problem with SDL (Undefined Reference)

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

## Recommended Posts

Hi, Im relatively new to programming. I know quite a bit about C++ and want to try to make a game so I started doing Lazy Foo's SDL tutorial.
Im using CodeBlocks 10.05 with GNU GCC compiler.
I got to Tutorial 4 and now Im completely stuck.

All that ever happens is I get the error "Undefined Reference to IMG_Load"

obj\Debug\source3.o||In function Z10load_imageSs':|
||=== Build finished: 1 errors, 0 warnings ===|

My linker options are set up as:
-lmingw32
-lSDLmain
-lSDL_image
-lSDL

I have the SDL.dll with the project folder
In the projects bin/Debug folder I have 6 .dll's and a .lib
libjpeg-8.dll
libpng15-15.dll
libtiff-5.dll
libwebp-2.dll
SDL_image.dll
zlib1.dll
SDL_image.lib

In the source files properties, I have Compile File, Link File, Debug, and Release all enabled, the SDL headers are included, and all the directories to SDL files and folders are all correct.

I can attach the source file if it helps at all.
Any help is really appreciated. I have been searching for days, hours on end for a solution and most posted solutions are changing the settings to something I already have set up.

##### Share on other sites
show some source code?

##### Share on other sites
Try putting -lSDL_image at the top. The problem is you are trying to use IMG_Load(), which is defined in SDL_Image. The linker can be a bit stupid with Code::Blocks, so you might have to rearrange the options.

Also, I don't see a need for SDL_image.lib to be in you output directory. Edited by bluehailex

##### Share on other sites
Thanks for the replies, here is the source code
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

//The even structure that will be used
SDL_Event event;

{

//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;

{
//Create an optimized image

//Free the old image
}

//Return the optimized image
return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;

//Get the offsets
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}

bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}

//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}

//set the window caption
SDL_WM_SetCaption( "Event test", NULL );

//If everything initialized fine
return true;
}

{

if( image == NULL )
{
return false;
}

return true;
}

void clean_up()
{
//Free the image
SDL_FreeSurface( image );

//Quit SDL
SDL_Quit();
}

int main( int argc, char* args[] )
{
//Make sure the programs waits for a quit
bool quit = false;

//Initialize
if( init() == false )
{
return 1;
}

{
return 1;
}

//Apply the surface to the screen
apply_surface( 0, 0, image, screen );

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}

//While the user hasn't quit
while( quit == false )
{
//While there's an even to handle
while( SDL_PollEvent( &event ) )
{
//If the user has X'd out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
}

//Free the surface and quit SDL
clean_up();

return 0;
}
[/source]

I tried putting -lSDL_image at the top and it didnt change anything :\

##### Share on other sites
It is possible you are linking with the wrong SDL_image library. You might be using an x64 build, when you really are x86. Or you may have even downloaded the wrong SDL_image zip all together from [url="http://www.libsdl.org/projects/SDL_image/"]here[/url]. I don't think there is anything wrong with your code.

##### Share on other sites
Thanks so much for the replies so far

##### Share on other sites
Try linking directly with the libraries by supplying the absolute path to them instead of giving the linker options. For example: "C:\Dev\Lib\SDL\SDL_image.lib" Make sure you put the paths in the "Link Libraries" section, not "Other linker options".

##### Share on other sites
Is it possible just somehow it is a lowercase L instead of an capital i ? I know its a far shot lol

##### Share on other sites
I know its not a capital i lolz xD

bluehailex your solution seemed to work!
Except now it just closes instantly with the message:
"Process terminated with status 1 (0 minutes, 0 seconds)"
So now I need to like look around and try to find out that problem!
Thanks so much! If you can maybe offer some advice for the current problem or using SDL in general, thatd be great

##### Share on other sites
Either something didn't build quite right, or CodeBlocks cannot find your output executable. Try running your program from the explorer.

As for SDL in general, it is a great library. Very portable, but it doesn't come with the greatest performance out of the box. It uses software rendering, as opposed to hardware rendering (using the GPU). If you are planning on doing your own rendering in OpenGL, it will work out very well. However, there is a similar library called SFML. It uses hardware rendering by default, and will give you great performance compared to SDL. I haven't used SDL in a while, so things may have changed. SFML was written in C++ and is object oriented, while SDL was written in C and is not object oriented.

SDL and SFML both support Windows/Linux/Mac/FreeBSD, but SDL goes even further to support consoles and even some mobile operating systems. SFML is pretty active, so I would not be surprised if it gets ported to consoles/mobile devices as well.

##### Share on other sites
Thanks so much for all the help [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Would you recommend any in particular? Like which one is easier for someone to pickup and go with?
Im not too concerned with mobile though [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Edit:
Toying around a bit, I found that the build error is coming in the load_files function, namely it is returning false no matter what.
I had this before in another load_image instance and I just had to remake the file, but it isn't working here. Any tips? lolz xP
Edit:
Nevermind! Got it, I had the x.png in the debug folder, when it needed to be in the project folder!
Thank you SO much!! You have no idea how awesome it is to have this working xD Edited by DaShwimp

##### Share on other sites
Glad I could help. SFML is probably going to be easier for you to learn, but once you built a simple framework around SDL or SFML it wont matter.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628662
• Total Posts
2984095

• 10
• 9
• 9
• 10
• 21