SDL2 - 未定义的引用 - Ubuntu

时间:2013-08-18 23:30:12

标签: ubuntu reference sdl undefined

我刚开始使用SDL2(在Ubuntu 13.04 x64上),一切都很顺利,直到我尝试使用SDL_image。

我添加了这段代码,用于加载图片:

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

我链接-lSDL2和-lSDL2_image,我得到了这个未定义的引用:

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您下载并编译/安装了这些库吗? 试试这个:

sudo apt-get install libsdl-dev

之后,您可以使用以下命令编译和链接SDL:

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

答案 1 :(得分:0)

更改库的顺序可能会解决问题。

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

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