Zac*_*iaz 3 c gcc cc segmentation-fault sdl-2
发布的代码直接从流行的SDL2教程示例复制而来,以确保不是我犯了一些愚蠢的错误。我对示例所做的全部工作是更改有问题的图像文件的路径,我将类型bool更改为int,将false更改为0,将true更改为1。据我所知,不应保留任何C ++特定的内容。
无论我做什么,一切似乎都可以正常工作,但是在使用CC / GCC进行编译时(我想这实际上是同一笔交易),我怀疑在close()中最终会遇到分段错误,但我无法确定。使用G ++进行编译可以防止分段错误。
解决方案当然很简单,只需使用G ++,但我非常想知道问题出在哪里。
main.c:
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
int init();
//Loads media
int loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
int init()
{
//Initialization flag
int success = 1;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = 0;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n",
SDL_GetError() );
success = 0;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
int loadMedia()
{
//Loading success flag
int success = 1;
//Load splash image
gHelloWorld = SDL_LoadBMP( "hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "hello_world.bmp",
SDL_GetError() );
success = 0;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Free resources and close SDL
close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我看不出它的相关性,但是为了安全起见,这是我正在使用的标准Makefile
#OBJS specifies which files to compile as part of the project
OBJS = main.c
#CC specifies which compiler we're using
CC = cc
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -Wall -Wextra -pedantic
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = test
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
Run Code Online (Sandbox Code Playgroud)
我没有收到任何错误或警告,但未使用argv和argc。
在这一点上,我处于死胡同,因此我在这里问。
最好的祝福。
注意:应该指出的是,无论是什么问题,这绝对不是硬件问题,在两个完全不同的硬件平台上,当我得到完全相同的结果和问题时,各种搜索结果都提出了建议。
如果重命名该功能,close()则段错误会消失,就像init()对SDL的调用,对X11的调用,对X11的调用,对驱动程序的调用close(),但对调用的调用不是对的,而是对close()您的调用。在C ++中,函数会将名称改成其他名称,所以这不是问题。