编译用C编写的SDL程序。

时间:2014-05-18 12:21:34

标签: c linux sdl

编译SDL程序时遇到问题。我根据这篇文章安装了SDL dev包:https://askubuntu.com/questions/344512/what-is-the-general-procedure-to-install-development-libraries-in-ubuntu(方法1)。它似乎完成没有任何错误。当我试图用这样的指令编译它时:gcc -I/usr/include/SDL/ showimage.c -o out -L/usr/lib -lSDL

它返回错误:

showimage.c:7:23: fatal error: SDL_image.h: No such file or directory compilation terminated.

即使我将SDL_image.h放到我编译的文件夹中,它也会返回此错误:

/tmp/ccFiSO10.o: In function `Load_image':
showimage.c:(.text+0xd): undefined reference to `IMG_Load'
collect2: ld returned 1 exit status

以下是我从老师那里听到的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "SDL.h"
#include "SDL_image.h"

SDL_Surface* Load_image(char *file_name)
{
        /* Open the image file */
        SDL_Surface* tmp = IMG_Load(file_name);
        if ( tmp == NULL ) {
            fprintf(stderr, "Couldn't load %s: %s\n",
                    file_name, SDL_GetError());
                exit(0);
        }
        return tmp;
(....)

我知道这段代码没问题,因为我的朋友编译了它并且工作正常。

有人可以帮我编译吗?

2 个答案:

答案 0 :(得分:1)

您看到的第一个错误是因为包含路径错误。使用-I标志将包含路径设置为GCC。由于包含路径不正确,因此无法找到您提及的SDL标头。

要解决此问题,请将-I设置为包含您要使用的标头的目录。

第二个错误来自链接器,它找不到IMG_load符号。此符号包含在SDL库中,需要将这些库提供给链接器才能找到符号。

要解决此问题,您需要将-L设置为包含SDL库文件的目录,并且还需要使用-l来提供要链接的库的名称。

有一个名为pkg-config的工具,它会在给定库名的情况下为您提供正确的-I -L和-l行。尝试运行pkg-config --cflags --libs sdl SDL_image

- cflags将为您提供include参数, - libs将为您提供库目录和名称。

您可以在gcc调用中包含命令,如下所示:

gcc `pkg-config --cflags --libs sdl SDL_image` showimage.c -o out

答案 1 :(得分:0)

在链接器中添加lSDL_image库。

相关问题