4

我刚开始使用 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

我究竟做错了什么??

4

2 回答 2

2

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

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

参考:链接器告诉我它无法解析符号,但它们在那里?

于 2013-08-26T16:09:03.667 回答
1

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

sudo apt-get install libsdl-dev

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

gcc `pkg-config --libs --cflags sdl` <sourcefile.c>
于 2013-08-19T17:14:48.063 回答