CC / GCC但不是G ++的奇数分段错误(C / SDL2 / Linux)

发布的代码直接从流行的SDL2教程的示例中复制,以确保不是我犯了一些愚蠢的错误。 我对示例所做的只是更改有问题的图像文件的路径,我将类型bool更改为int,将false更改为0并将true更改为1.据我所知,没有任何特定于C ++的内容。

无论我做什么,一切似乎都有效,但是当用CC / GCC进行编译时(我认为这真的是同样的交易)我最终得到了一个分段错误,我怀疑是close(),我一直无法确定。 用G ++编译可以防止分段错误。

解决方案当然很简单,只需使用G ++,但我非常想知道问题所在。

main.c中:

//Using SDL and standard IO #include  #include  //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。

在这一点上,我处于死胡同,因此我在这里问。

最好的祝福。

注意:我应该提到的是,无论出现什么问题,它绝对不是硬件问题,各种搜索结果都表明,在两个完全不同的硬件平台上,我得到完全相同的结果和问题。

如果你重命名函数close() ,segfault会消失,看起来像是调用SDL的init()调用X11调用你的调用close()驱动程序,而不是调用右边的close()而是调用你的调用。 在C ++函数中,名称会被修改为其他内容,所以它不是问题。