Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Problem with SDL (Undefined Reference)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 DaShwimp   Members   -  Reputation: 108

Like
0Likes
Like

Posted 07 August 2012 - 03:12 PM

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! Posted Image

Sponsor:

#2 dimitri.adamou   Members   -  Reputation: 329

Like
0Likes
Like

Posted 07 August 2012 - 03:44 PM

show some source code?

#3 bluehailex   Members   -  Reputation: 143

Like
0Likes
Like

Posted 07 August 2012 - 04:41 PM

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, 07 August 2012 - 04:44 PM.


#4 DaShwimp   Members   -  Reputation: 108

Like
0Likes
Like

Posted 07 August 2012 - 05:08 PM

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 attributesconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//The surfacesSDL_Surface *image = NULL;SDL_Surface *screen = NULL;//The even structure that will be usedSDL_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 :\

#5 bluehailex   Members   -  Reputation: 143

Like
0Likes
Like

Posted 07 August 2012 - 10:56 PM

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 here. I don't think there is anything wrong with your code.

#6 DaShwimp   Members   -  Reputation: 108

Like
0Likes
Like

Posted 07 August 2012 - 11:57 PM

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 :)

#7 bluehailex   Members   -  Reputation: 143

Like
1Likes
Like

Posted 08 August 2012 - 06:13 PM

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".

#8 dimitri.adamou   Members   -  Reputation: 329

Like
0Likes
Like

Posted 08 August 2012 - 06:57 PM

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

#9 DaShwimp   Members   -  Reputation: 108

Like
0Likes
Like

Posted 08 August 2012 - 07:17 PM

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 :)

#10 bluehailex   Members   -  Reputation: 143

Like
1Likes
Like

Posted 08 August 2012 - 09:07 PM

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.

#11 DaShwimp   Members   -  Reputation: 108

Like
0Likes
Like

Posted 09 August 2012 - 12:02 AM

Thanks so much for all the help Posted Image

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 Posted Image

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, 09 August 2012 - 12:15 AM.


#12 bluehailex   Members   -  Reputation: 143

Like
0Likes
Like

Posted 09 August 2012 - 08:18 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS