時出現分段錯誤(核心轉儲)這裏是我的代碼。
任何人都可以幫助我嗎?爲什麼在編譯我的代碼C(linux)
#include <SDL/SDL.h>
#include <stdlib.h>
#include <stdio.h>
int blit_image(char chemin[15],SDL_Surface *fenetre,int posx,int posy) {
SDL_Surface *temp;
SDL_Surface *image;
SDL_Rect position;
temp = SDL_LoadBMP(chemin);
image= SDL_DisplayFormat(temp);
position.x = posx;
position.y = posy;
position.w = image->w;
position.h = image->h;
SDL_BlitSurface(image,NULL,fenetre,&position); SDL_Flip(fenetre);
SDL_FreeSurface(temp);
SDL_FreeSurface(image);
}
int main(int argc, char *argv[]) {
SDL_Surface *fenetre;
SDL_Event event; int done=1; SDL_Init(SDL_INIT_VIDEO);
fenetre = SDL_SetVideoMode(910,600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
blit_image("resources/images/fondblanc.bmp",fenetre,0,0);
blit_image("resources/images/chat.bmp",fenetre,0,0);//cat
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);//dog
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); //horse
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); //tiger
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);//hen
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
while (done) {
SDL_WaitEvent(&event);
switch(event.type) {
case SDL_QUIT:
{
done=0;
break;
}
case SDL_MOUSEBUTTONDOWN:
{
if ((event.button.x>820)&&(event.button.x<890)&&(event.button.y<115)&& (event.button.y>25))
{
blit_image("resources/images/chat.bmp",fenetre,0,0);
}
if ((event.button.x>820)&&(event.button.x<890)&&(event.button.y<230)&& (event.button.y>140))
{
blit_image("resources/images/chien.bmp",fenetre,0,0);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<345)&&(event.motion.y>255))
{
blit_image("resources/images/cheval.bmp",fenetre,0,0);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<460)&& (event.motion.y>370))
{
blit_image("resources/images/tigre.bmp",fenetre,0,0);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<575)&&(event.motion.y>485))
{
blit_image("resources/images/poule.bmp",fenetre,0,0);
}
}
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<115)&& (event.motion.y>25))
{
blit_image("resources/bouttons/chatbtClic.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<230)&&(event.motion.y>140))
{
blit_image("resources/bouttons/chienbtClic.bmp",fenetre,820,140);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<345)&& (event.motion.y>255))
{
blit_image("resources/bouttons/chevalbtClic.bmp",fenetre,820,255);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<460)&&(event.motion.y>370)) {
blit_image("resources/bouttons/tigrebtClic.bmp",fenetre,820,370);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485);
}
if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<575)&&(event.motion.y>485))
{
blit_image("resources/bouttons/poulebtClic.bmp",fenetre,820,485);
blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);
blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140);
blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255);
blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);
}
}
SDL_Quit();
return 0;
}
我用gcc game.c -o prog -lSDL -lSDL_mixer -lSDL_ttf
代碼編譯succeffully但上鍵入./prog
執行它顯示我"Segmentation fault (core dumped)"
代碼編譯它尚未存在於目錄中使用的文件
在像gdb這樣的調試器中運行該程序,並查看它崩潰的位置 – iain
*「分段錯誤(核心轉儲)」*不是編譯器錯誤(除非發現編譯器錯誤,這是不太可能的)。相反,它更可能是你的*運行時*錯誤。描述你的確切步驟。你編譯過代碼嗎?你鏈接的代碼?你是否運行過代碼?你究竟在哪裏得到錯誤,你是如何嘗試調試它的? – abelenky
我使用gcc game.c編譯了它-o prog -lSDL -lSDL_mixer -lSDL_ttf – sanaEsprit