Sylar
Sylar

Reputation: 357

SDL2 - Undefined references - Ubuntu

I just started using SDL2 (on Ubuntu 13.04 x64) and everything was going fine, until I tried to use SDL_image.

I added this piece of code, for loading images:

SDL_Texture* LoadImage(std::string file)
{
    SDL_Texture* tex = NULL;
    tex = IMG_LoadTexture(ren, file.c_str());
    if (tex == NULL)
        throw std::runtime_error("Failed to load image: " + file + IMG_GetError());
    return tex;
}

I link with -lSDL2 and -lSDL2_image, and I get this undefined references:

g++  -o "SDL"  ./src/main.o   -lSDL2 -lSDL2_image
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_malloc"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_memcmp"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_strncasecmp"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_memset"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_free"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_memcpy"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_sscanf"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_isspace"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_realloc"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_strcmp"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_strncmp"
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libSDL2_image.so: riferimento non definito a "SDL_snprintf"
collect2: error: ld returned 1 exit status

What am I doing wrong??

Upvotes: 4

Views: 2974

Answers (2)

gabomdq
gabomdq

Reputation: 1750

It's possible that changing the order of the libraries will solve the problem.

g++  -o "SDL"  ./src/main.o -lSDL2_image -lSDL2 

Ref: linker tells me it can't resolve symbols, but they're there?

Upvotes: 2

Mohammad Izady
Mohammad Izady

Reputation: 427

Have you downloaded and compiled/installed the libraries? Try this:

sudo apt-get install libsdl-dev

After that you can compile and link against SDL with:

gcc `pkg-config --libs --cflags sdl` <sourcefile.c>

Upvotes: 1

Related Questions