Le code posté est directement copié d’un exemple de tutoriel populaire SDL2, afin de garantir que ce n’est pas moi qui ai commis une erreur stupide. Tout ce que j’ai fait avec l’exemple, c’est de changer le chemin du fichier image en question. J’ai changé le type bool en int, false en 0 et true en 1. Si je comprends bien, rien de spécifique à C ++ ne devrait restr.
Tout semble fonctionner, peu importe ce que je fais, mais lors de la compilation avec CC / GCC (je suppose que c’est vraiment la même affaire), je reçois un défaut de segmentation à la fin, je soupçonne bien dans close (), que je n’ai pas pu déterminer . La compilation avec G ++ évite en quelque sorte l’erreur de segmentation.
La solution est bien sûr simple: utilisez simplement G ++, mais j’aimerais beaucoup savoir où réside le problème.
principal 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; }
Je ne vois pas en quoi cela est pertinent, mais pour plus de sécurité, voici le Makefile standard que j’utilise
#OBJS specifies which files to comstack as part of the project OBJS = main.c #CC specifies which comstackr 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 comstacks our executable all : $(OBJS) $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
Je ne reçois aucune erreur ni aucun avertissement, mais argv et argc non utilisés.
À ce stade, je suis dans une impasse, donc je demande ici.
Meilleures salutations.
NB: Il convient de mentionner que quel que soit le problème, il ne s’agit certainement pas d’un problème lié au matériel, mais plusieurs résultats de recherche l’ont suggéré, car le résultat et le problème sont identiques, sur deux plates-formes matérielles totalement différentes.
Si vous renommez la fonction close()
le segfault disparaît, ressemble à un appel init()
dans SDL qui appelle à X11 qui appelle votre pilote qui appelle close()
, mais au lieu d’appeler le droit close()
il appelle le vôtre En C ++, les fonctions seront mutilées dans leur nom, ce qui ne pose pas de problème.