SDL - 分段错误(核心转储),有什么想法吗?

时间:2014-12-01 14:32:19

标签: c ubuntu

自从我安装了SDL以来出现了这个问题。首先,我尝试用tar.gz文件安装它,在尝试编译时没有用(终端找不到SDL lib的目录),所以之后我安装了synpatic包mng,并成功下载了“libsdl1.2-dev”文件。 我正在关注lazzy foo的SDL教程,每当我尝试编译一个简单的代码来创建一个屏幕并blit一个图像时,我在终端中得到以下消息:

(gcc -Wall -o teste teste.c -lSDL -lSDL_image)

“分段错误(核心转储)”

这是我在C代码中的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include "SDL/SDL.h" 

    int main( int argc, char* args[] ) 
    {
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

    if (screen == NULL) {
        printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
        exit(1); /* Unrecoverable error */
    }

    hello = SDL_LoadBMP("hello.bmp");

    SDL_BlitSurface(hello, NULL, screen, NULL);

    SDL_Flip(screen);

    SDL_Delay(2000);

    SDL_FreeSurface(hello);

    SDL_Quit();

    return 0;
    }
  • 我已经确定hello.bmp与我的teste.c文件位于同一个目录中。

这是使用gdb进行回溯的日志:

LOG

GNU gdb (Ubuntu 7.8-1ubuntu4) 7.8.0.20141001-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from teste...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/lazzo/Documentos/Treino/teste 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff707c700 (LWP 5605)]

Program received signal SIGSEGV, Segmentation fault.
SDL_Flip (screen=0x0) at ./src/video/SDL_video.c:1109
1109    ./src/video/SDL_video.c: No such file or directory.
(gdb) bt
#0  SDL_Flip (screen=0x0) at ./src/video/SDL_video.c:1109
#1  0x00000000004009a2 in main ()
(gdb) c
Continuing.
[Thread 0x7ffff7fd8740 (LWP 5601) exited]

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) q
]0;lazzo@J-Ubuntu: ~/Documentos/Treinolazzo@J-Ubuntu:~/Documentos/Treino$ exit
exit

结束日志

你们可以给我的任何帮助都会非常感激,我为我的英语不好而道歉,我来自巴西并且还在学习英语。

更新

将Klas建议添加到我的代码后,我从终端获得了这个:

“SDL_SetVideoMode失败:没有可用的视频设备”

怎么可能呢? (我的显卡是radeon HD 4850 btw)

2 个答案:

答案 0 :(得分:2)

第1轮问题(编译):

目标文件名必须紧跟在-o选项之后,因此您应该更改参数的顺序:

gcc -Wall -o teste teste.c -lSDL -lSDL_image

这可能无法解决您的所有构建问题,但这是一个良好的开端。

第2轮问题(添加错误处理):

SDL_SetVideoMode的调用返回null。如果返回值为null,则应立即调用SDL_GetError以检查错误:

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if (screen == NULL) {
    printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
    exit(1); /* Unrecoverable error */
}

您应该为其他SDL调用添加类似的处理。

答案 1 :(得分:0)

在我的案例中,唯一能解决的问题是格式化Ubuntu并尝试另一个发行版。现在我正在使用Linux Mint,尽管它完全基于Ubuntu,但现在一切正常。只是分享我对问题的解决方案,以防其他人有一天遇到同样的问题。