【问题标题】:SDL - Segmentation Fault (core dumped), any thoughts?SDL - 分段错误(核心转储),有什么想法吗?
【发布时间】:2014-12-11 07:04:56
【问题描述】:

自从我安装了 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 进行回溯的日志:

日志

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)

【问题讨论】:

  • 语法:$ gcc [选项] [源文件] [目标文件] -o 输出文件
  • 输出文件名应该紧跟-o选项。
  • SDL_SetVideoMode 的调用返回null。如果你得到null的返回值,你应该立即调用SDL_GetError来检查错误是什么。
  • 感谢您的回答。你介意指出我应该怎么做吗?请?谈到 SDL,我真的是新手,以防万一还不清楚。 ://
  • 我在答案中添加了一个示例。

标签: c ubuntu


【解决方案1】:

第一轮问题(编译):

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

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

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

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

SDL_SetVideoMode 的调用返回空值。如果你得到一个 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 调用添加类似的处理方式。

【讨论】:

  • 谢谢,我已经按照确切的顺序更改了语法,但是现在,当尝试执行程序 (./teste) 时,我得到的只是:“分段错误(核心转储)”跨度>
  • @Lazzo 那将是一个单独的问题(你也应该看看使用gdb 或其他一些调试器)
  • 谢谢,我已经完全编辑了我的问题,以使其与我遇到的新问题相匹配。我看不出有必要创建一个全新的页面,因为我仍然遇到问题。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多