[英]SIGSEGV error in c++ SDL
這段代碼使SIGSEGV錯誤,我不知道為什么。
#include<SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main(int argc, char* args[])
{
// initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,640, 480,SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if(g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return 1; // sdl could not initialize
}
// everything succeeded lets draw the window
// set to black // This function expects Red, Green, Blue and
// Alpha as color values
SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
// show the window
SDL_RenderPresent(g_pRenderer);
// set a delay before quitting
SDL_Delay(5000);
// clean up SDL
SDL_Quit();
return 0;
}
此代碼來自Sean Mitchell撰寫的“ SDL游戲開發”一書。 但是我沒有使用本書中建議的Visual Studio,而是使用mingw。 我已經按照lazyfoo的教程中的說明配置了所有內容: http ://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/mingw/index.php他的“ Hello SDL”工作正常。 這是我的Makefile:
#OBJS specifies which files to compile as part of the project
OBJS = main.cpp
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = hello
#This is the target that compiles our executable
all : $(OBJS)
g++ $(OBJS) -IC:\Artur\Projects\SDL\include\SDL2 -LC:\Artur\Projects\SDL\lib -w -lmingw32 -lSDL2main -lSDL2 -o $(OBJ_NAME) -g
我在gdb中發現的東西:
(gdb) run
Starting program: C:\Artur\Projects\CPP\Snake/hello.exe
[New Thread 4800.0x45c]
[New Thread 4800.0xc7c]
[New Thread 4800.0xc48]
[New Thread 4800.0xa8c]
[New Thread 4800.0xbc0]
[New Thread 4800.0x1350]
Breakpoint 1, SDL_main (argc=argc@entry=1, args=args@entry=0x3b0008)
at main.cpp:17
17 g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
(gdb) s
[New Thread 4800.0xf98]
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? <>
所以我知道SDL_CreateRenderer函數中的問題,但我不知道出了什么問題。
由於這一行,您遇到了細分錯誤-
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
原因是g_pWindow
為NULL
。 當您在這一行中使用NULL
進行初始化時-
SDL_Window* g_pWindow = 0;
您需要提供SDL_Window
有效指針。
有關詳細信息,請檢查此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.