发布的代码直接从流行的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;
}
我不知道它是如何相关的,但只是为了安全起见,这是我正在使用的标准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。
此时我已经死路一条,所以我在这里问。
最好的问候。
NB: 我应该提到的是,无论出现什么问题,它绝对不是硬件问题,各种搜索结果都表明,因为我在两个完全不同的硬件平台上得到完全相同的结果和问题。
答案 0 :(得分:5)
如果重命名函数close()
,段错误就会消失,看起来像init()
调用SDL调用X11调用驱动程序调用{{1}},而不是调用右close()
它会调用你的。在C ++中,函数会被命名为其他东西,所以它不是问题。