Sign in to follow this  

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.

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

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':|
C:\Users\Da Shwimpster\Documents\C++\Platformer\Platformer\source3.cpp|27|undefined reference to `IMG_Load'|
||=== 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.
Thanks in advanced! [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Share this post


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


Link to post
Share on other sites
Thanks for the replies, here is the source code
[source lang="cpp"]//The headers
#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;

SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

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

//Load the image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old image
SDL_FreeSurface( loadedImage );
}

//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;
}

bool load_files()
{
//Load the image
image = load_image( "x.png" );

//If there was an error in loading the image
if( image == NULL )
{
return false;
}

//If everything loaded fine
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;
}

//Load the files
if( load_files() == false )
{
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 this post


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


Link to post
Share on other sites
I downloaded the 3rd download under Windows Binary, SDL_image-devel-1.2.12-VC.zip as the tutorial instructed me to. Im using a 32bit compiler and made sure to use the x86.
Thanks so much for the replies so far :)

Share this post


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


Link to post
Share on other sites
I know its not a capital i lolz xD

bluehailex your solution seemed to work! :D
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 this post


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


Link to post
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!! :D You have no idea how awesome it is to have this working xD Edited by DaShwimp

Share this post


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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this