user3290771
user3290771

Reputation: 21

Code::Blocks SDL2.0 "undefined reference to 'IMG_Load'"

I'm trying to use c++, SDL, and SDL-image to make a game; I'm using Code::Blocks as my IDE, and I'm having some trouble.

I've downloaded the latest mingw SDL development files, and the latest mingw SDL-image development files.

I've placed all the SDL-image stuff into the same directory as the SDL stuff (merging the one into the other).

I've added the x86_64-w64-mingw32/include and x86_64-w64-mingw32/lib directories to my Code::Blocks search directories.

My linker settings are as follows:

-lmingw32
-lSDL2main
-lSDL2
-lSDL2_image
-lopengl32
-lglu32

(Obviously I'm using openGL as well, but I don't think that's involved here).

I have the correct #includes as far as I can tell:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_opengl.h>

But I'm still getting the error: undefined reference to 'IMG_Load'

Here's the build log:

obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0xd7): undefined reference to `IMG_Load'
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0x1dd): undefined reference to `SDL_FreeSurface'
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0x1f4): undefined reference to `SDL_FreeSurface'
g:/program files(x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: obj\Release\gfx_Texture.o: bad reloc address 0x4 in section `.text.startup'
g:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation

And the actual code:

SDL_Surface* image = IMG_Load(file);

Where file is a char*.

I understand that there have been some changes to the way SDL_Surfaces work in SDL 2.0 as compared to SDL 1.2 (I'm trying to migrate, both from 1.2 to 2.0 and from MVSC to C::B), so could that have something to do with it? Am I using IMG_Load wrong?

Your help is much appreciated, and I'll supply any missing info on request if it'll clarify anything.

One more thought: The latest version of SDL is 2.0.1, yet the latest version of SDL_image is 2.0.0. Do I need those versions to match?

Upvotes: 1

Views: 5155

Answers (3)

Firaol
Firaol

Reputation: 1

I encountered such an error, and all I did was check if my linker texts are correctly written check if you have typed "ISDL2_image" instead of "lSDL2_image" (write small 'L' then SDL2 not cap 'I' then SDL2)for SDL2 projects. for SDL1 just change the '2's into '1's. 👍

Upvotes: 0

Miguel
Miguel

Reputation: 408

This happen When the program doesn't linker correctly at the executed time

for example: asas@asas:~/SDL$ g++ -Wall SDL_program.cpp -lSDL2
/tmp/cc1eC6CK.o: InfunctionLTexture::loadFromFile(std::__cxx11::basic_string<char,std::char traits<char>, std::allocator<char> >)': SDL_program.cpp:(.text+0x7d): **undefined reference toIMG_Load'**

As you can see above miss linker -lSDL2_image .

Is possible too, that you have SDL1.2 and SDL2 at the same root and again Code blocks hasn't linked correctly

Upvotes: 2

user3290771
user3290771

Reputation: 21

Well don't I feel dumb. When I was working in MVC the x86 prefix meant 32-bit; for some reason, it means 64-bit in C::B, and the i686 prefix means 32-bit.

Upvotes: 1

Related Questions