【发布时间】: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,我真的是新手,以防万一还不清楚。 ://
-
我在答案中添加了一个示例。