• Advertisement
Sign in to follow this  

SDL IMG_Load

This topic is 4344 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.. I'm currently reading the Lazy Foo productions tutorial on SDL and can't get it to work, the image extension library that is. I've extracted the necessary lib files, dll files and the include file to the right folders. I've also extracted all the dll files into the system32 folder, i'm using the paramter -ISDL_image and yes I have the other parameters too (-lmingw32 -lSDLmain -lSDL). I don't get any compiler errors, but a linker error which is the following: undefined reference to `IMG_Load' I'm using dev c++ and this is the only code block that use IMG_Load.
[SOURCE]
SDL_Surface *load_image(std::string filename) {
    //Temporary storage for 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 nothing went wrong in loading the image
    if (loadedImage != NULL) {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat(loadedImage);

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

    return optimizedImage;
}
[/SOURCE]
I have searched around everywhere but haven't found anything that could help me, thankful for help..

Share this post


Link to post
Share on other sites
Advertisement
Well, you also need to link the SDL_Image, that is,
-lSDL_Image
(note: that is a little el, not a big ai)

Share this post


Link to post
Share on other sites
Like I said, i've already linked to that, do you know any other reason it could be like this?

Share this post


Link to post
Share on other sites
You could try rearranging the order of your linked libs. That makes a difference for MinGW I think.

Sorry I can't be more specific.

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
You could try rearranging the order of your linked libs. That makes a difference for MinGW I think.

Sorry I can't be more specific.


The perfect order is:

-lmingw32
-lSDLmain
[other sdl libs]
-lSDL
[if you have any opengl stuff]


Atleast it works for me :).

Share this post


Link to post
Share on other sites
This is the complete parameter list in order i'm using:
-lmingw32 -lSDLmain -lSDL -ISDL_Image

prog.cpp: undefined reference to `IMG_Load'

This is really frustruating, can't continue reading if it doesn't become right. It's only one function and that is IMG_Load, I hate illogical errors like these that should be right, but are filled with errors even so.

Well, anyways, if someone knows anything about this, please post here, will search around some more about it.

Share this post


Link to post
Share on other sites
Sharlin already told you the solution!

use
-lSDL_Image
instead of
-ISDL_Image

(have a closer look at the first character before "SDL_Image"....)

Share this post


Link to post
Share on other sites
Ok, to elaborate:

* The -Idirectory (big ai) argument tells the compiler to search for header files from that directory in addition to the default ones.

* But the -llibrary (little ell) argument, instead, tells the linker to link the given library to the executable.

In this case, you need both of those arguments for the compiling to succeed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
Ok, to elaborate:

* The -Idirectory (big ai) argument tells the compiler to search for header files from that directory in addition to the default ones.

* But the -llibrary (little ell) argument, instead, tells the linker to link the given library to the executable.

In this case, you need both of those arguments for the compiling to succeed.


I didn't.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No really, the linker parameters should be like this:

-lmingw32
-lSDLmain
-lSDL
-lSDL_image

Notice the little L at the start in each case.
Also make sure you've extracted the bits of the image package appropriately..

SDL_image.lib from SDL_image-1.2.4/lib to Dev-Cpp/lib

Share this post


Link to post
Share on other sites
Sorry, didn't understand that it was supposed to be a l instead of an i. Anyways the error disappeared now, but it says that it doesn't exists instead (linker error).

That's really strange because it didn't say that when the paramter was "i", which is the one that should return that message. Now when the right parameter is in place it returns the doesn't exist error, really odd.

Share this post


Link to post
Share on other sites
Quote:
Original post by password
Sorry, didn't understand that it was supposed to be a l instead of an i. Anyways the error disappeared now, but it says that it doesn't exists instead (linker error).

That's really strange because it didn't say that when the paramter was "i", which is the one that should return that message. Now when the right parameter is in place it returns the doesn't exist error, really odd.


you forgot this step:
Quote:
From the tutorial
Next extract the lib file that's inside of lib subfolder of the archive to the Dev C++ lib folder.

The Dev C++ lib folder should be at C:\Dev-Cpp\lib.


and it's not strange. The first time it was complaining that you never told it where the function was (which is what a lib file does), and now it's complaining it can't find the lib file you're trying to link against.

Share this post


Link to post
Share on other sites
that means SDL_image.lib is not in C:\DEV-C_~1\lib or you put -l-lSDL_image

double check.

Share this post


Link to post
Share on other sites
Does mingw load <whatever>.lib files with the -l parameter? I thought it only loaded lib<whatever>.a files, maybe Im wrong.

For SDL_Image, I've always used the file libSDL_image.a (not SDL_image.lib, i may have downloaded a devpack or compiled it myself, i can't remember, i installed it long ago...) and have never had problems. For .lib's I've always added the full path (eg: whatever/dev-cpp/lib/foo.lib instead of -lfoo or if the file was libfoo.a i use -lfoo).

Share this post


Link to post
Share on other sites
Quote:
Original post by Lazy Foo
that means SDL_image.lib is not in C:\DEV-C_~1\lib or you put -l-lSDL_image

double check.


Maybe he DOES have THAT, but that won't matter. He probably doesn't have libSDL_Image.a.

[grin][wink]

Share this post


Link to post
Share on other sites
In my /DevCpp/lib dir, I have the files "SDL_ttf.lib" AND "libSDL_image.a" and both of them work the exact same way in the parameters tab - that is, I use -lSDL_ttf -lSDL_image. Works fine.

Share this post


Link to post
Share on other sites
I can test the libSDL_image.a file if you say that is working, but where can I get it?

Share this post


Link to post
Share on other sites
I'm pretty sure I got it from here using the "SDL_image-devel-1.2.4-VC6.zip" file. It has SDL_image.lib; I don't remember if I renamed this file to libSDL_image.a or not but I definitely do have libSDL_image.a in my compiler's lib folder. I guess that's what I did.

Hey: I tried renaming libSDL_image.a to SDL_image.lib and I could still compile projects dependent on SDL_image fine! Either way you'll need the file if you don't have it, but it looks like renaming it is optional. I wonder why I did it originally?

Share this post


Link to post
Share on other sites
Damn, it still doesn't work, why do these illogical errors keep bugging me wherever I go, they always appear where I am!

I've tried many things now, renamed the -lSDL_image parameter to these things:
-SDL_image
-SDL_image.lib
-lSDL_image.lib
-llibSDL_image.a
-libSDL_image.a
-lc:\dev\lib\SDL_image
-l\dev\lib\SDL_image

have also tried to rearrange the order countless times, with all those combinations aswell. It should find it, i've copied the file in every folder in dev-c++. It keeps saying that it doesn't find the file, these kind of errors seem to be unsolveable.

Share this post


Link to post
Share on other sites
I don't think you answered a couple of the questions above:
- Do you have libSDL_image.a?
- If so, is it in the correct directory?
- And where did you get it from?

I don't think you can get away with just renaming a .lib as a .a file. One is for Microsoft compilers and one is not.

Share this post


Link to post
Share on other sites
I originially thought that the differences between the library version (MSVC vs. MinGW) were your problem, but this says that it's a straight C library and the MinGW linker should be able to link against it.

I found this on the web about setting up SDL_image with MinGW at http://gpwiki.org/index.php/SDL:Tutorials:Setup

Quote:


MinGW and SDL_image


SDL_image is an invaluable add-on library that allows you to use a wide variety of image formats with SDL, including BMP, JPG, GIF, PNG, TGA, PCX and many more (as opposed to just BMPs with SDL alone).

If you want to get up and running quickly without re-compiling from source, simply grab the runtime development package. This time however, you can use the MSVC package. You can link directly to the SDL_image.lib file as it only requires plain vanilla C linkage (no C++ or other MinGW-specific WinMain). Just extract the archive to a location and remember to set the include and lib paths when you compile.

The documentation site for SDL_image is clear and straightforward. It also has an excellent example source to try which serves as a great little intro to both SDL and SDL_Image at the same time. Assuming your SDL/ and SDL_image/ directories are at the same level, create another folder at that same level, called viewimage. Grab the viewimage.c source from the demos directory, as well as a few image files to test. Put the files you download in the viewimage folder. Then you can use this makefile to build the test app:

make -f viewimage.makefile

MinIDE users will just build an SDL project as usual, but add the SDL_image.lib file to the project by dragging that file's icon into the target window, and adding ../SDL_image/include to the include path.

Finally, copy all the .dll files (zlib.dll, jpeg.dll, libpng1.dll, SDL_image.dll and SDL.dll) to the viewimage directory so the app can find them when you run it. The app is a command-line app, so pull up a command prompt and cd to that directory. Test it by running it with the name of one of the demo images:



Cheers,

Bob

[Edited by - Scourage on April 2, 2006 2:11:51 PM]

Share this post


Link to post
Share on other sites
I have had similar troubles statically linking to SDL_image across various compilers. Since it is one dll/so with one (useful) entry point, I would recommend just loading it dynamically.

In SDL, something like this (I haven't compiled it):

void* handle = SDL_LoadObject( "SDL_image" );
libIMG_Load = (PFN_IMG_LOAD) SDL_LoadFunction( handle, "IMG_Load" );

will find the IMG_Load for you. You will have to put in some #ifdefs to load the correct filename: SDL_image, SDL_image.so, or SDL_image.dylib. But overall I find, for this case, a dynamic load easier to get working.

lee

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement