发布的代码直接从一个流行的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: %sn", 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: %sn",
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: %sn", "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;
}
我不知道它有什么关系,但为了安全起见,这是我使用的标准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)
我没有得到任何错误或警告,但未使用的argv和argc。
在这一点上,我是在死胡同,所以我问这里。
致以最良好的问候。
注:我应该提到的是,无论是什么问题,它绝对不是硬件问题,各种搜索结果都表明,因为我在两个完全不同的硬件平台上得到了完全相同的结果和问题。
如果重命名函数close()
,segfault就会消失,看起来就像init()
调用SDL,SDL调用X11,X11调用您的驱动程序,驱动程序调用close()
,但它没有调用正确的close()
,而是调用您的。在C++中,函数的名称会被篡改为其他名称,所以这不是问题。