”,则可以在C++中得到相同的问题行为。,c,gcc,sdl,sdl-2,C,Gcc,Sdl,Sdl 2" /> ”,则可以在C++中得到相同的问题行为。,c,gcc,sdl,sdl-2,C,Gcc,Sdl,Sdl 2" />

SDL#U退出事件与g++;但不是gcc 我想用C来代替SDC++,但是我不能让事件运行。 如果它是一个.c或cpp文件并用g++编译,或者如果它是一个.cpp文件并用gcc编译,则该代码可以完美地工作。然而,如果它是一个.c文件,并且使用gcc编译,那么它编译得很好,但是SDL_QUIT事件实际上没有做任何事情,窗口只是永远挂起 #include<SDL2/SDL.h> #include <stdio.h> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 400; int init(); int loadMedia(); void mainLoop(); void close(); SDL_Window* window = NULL; SDL_Surface* screenSurface = NULL; SDL_Surface* helloWorld = NULL; int main( int argc, char* args[] ){ if( !init() ){ printf("Failed to initialize!\n"); } else{ if( !loadMedia() ){ printf("Failed to load media!\n"); } else{ mainLoop(); } } close(); return 0; } void mainLoop(){ int quit = 0; SDL_Event e; while( !quit ){ while( SDL_PollEvent( &e ) != 0 ){ if(e.type == SDL_QUIT){ quit = 1; } } SDL_BlitSurface( helloWorld, NULL, screenSurface, NULL); SDL_UpdateWindowSurface( window ); } } int init(){ int success = 1; //initialize sdl if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0){ printf("SDL could not initialize! SDL_Error: %s\n" , SDL_GetError() ); success = 0; } else { //create window window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( window == NULL ){ printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); success = 0; } else { screenSurface = SDL_GetWindowSurface( window ); } } return success; } int loadMedia(){ int success = 1; helloWorld = SDL_LoadBMP("images/helloWorld.bmp"); if( helloWorld == NULL ){ printf(" Unable to load image %s! SDL_Error: %s\n", "images/helloWorld.bmp", SDL_GetError() ); success = 0; } return success; } void close(){ //deallocate surface SDL_FreeSurface( helloWorld); helloWorld = NULL; //Destroy window SDL_DestroyWindow( window ); window = NULL; //Quit SDL subsystems SDL_Quit(); } #包括 #包括 //屏幕尺寸常数 屏幕宽度=640; 屏幕上的常数=400; int init(); int loadMedia(); void mainLoop(); 无效关闭(); SDL_Window*Window=NULL; SDL_表面*屏幕表面=空; SDL_曲面*helloWorld=NULL; int main(int argc,char*args[]{ 如果(!init()){ printf(“初始化失败!\n”); } 否则{ 如果(!loadMedia()){ printf(“加载媒体失败!\n”); } 否则{ mainLoop(); } } close(); 返回0; } void mainLoop(){ int-quit=0; SDL_事件e; 而(!退出){ while(SDL_PollEvent(&e)!=0){ 如果(e.type==SDL\u退出){ 退出=1; } } SDL_BlitSurface(helloWorld,NULL,屏幕表面,NULL); SDL_更新内表面(窗口); } } int init(){ int成功=1; //初始化sdl if(SDL_Init(SDL_Init_视频| SDL_Init_事件)外部”C“/Cuth>”,则可以在C++中得到相同的问题行为。

SDL#U退出事件与g++;但不是gcc 我想用C来代替SDC++,但是我不能让事件运行。 如果它是一个.c或cpp文件并用g++编译,或者如果它是一个.cpp文件并用gcc编译,则该代码可以完美地工作。然而,如果它是一个.c文件,并且使用gcc编译,那么它编译得很好,但是SDL_QUIT事件实际上没有做任何事情,窗口只是永远挂起 #include<SDL2/SDL.h> #include <stdio.h> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 400; int init(); int loadMedia(); void mainLoop(); void close(); SDL_Window* window = NULL; SDL_Surface* screenSurface = NULL; SDL_Surface* helloWorld = NULL; int main( int argc, char* args[] ){ if( !init() ){ printf("Failed to initialize!\n"); } else{ if( !loadMedia() ){ printf("Failed to load media!\n"); } else{ mainLoop(); } } close(); return 0; } void mainLoop(){ int quit = 0; SDL_Event e; while( !quit ){ while( SDL_PollEvent( &e ) != 0 ){ if(e.type == SDL_QUIT){ quit = 1; } } SDL_BlitSurface( helloWorld, NULL, screenSurface, NULL); SDL_UpdateWindowSurface( window ); } } int init(){ int success = 1; //initialize sdl if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0){ printf("SDL could not initialize! SDL_Error: %s\n" , SDL_GetError() ); success = 0; } else { //create window window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( window == NULL ){ printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); success = 0; } else { screenSurface = SDL_GetWindowSurface( window ); } } return success; } int loadMedia(){ int success = 1; helloWorld = SDL_LoadBMP("images/helloWorld.bmp"); if( helloWorld == NULL ){ printf(" Unable to load image %s! SDL_Error: %s\n", "images/helloWorld.bmp", SDL_GetError() ); success = 0; } return success; } void close(){ //deallocate surface SDL_FreeSurface( helloWorld); helloWorld = NULL; //Destroy window SDL_DestroyWindow( window ); window = NULL; //Quit SDL subsystems SDL_Quit(); } #包括 #包括 //屏幕尺寸常数 屏幕宽度=640; 屏幕上的常数=400; int init(); int loadMedia(); void mainLoop(); 无效关闭(); SDL_Window*Window=NULL; SDL_表面*屏幕表面=空; SDL_曲面*helloWorld=NULL; int main(int argc,char*args[]{ 如果(!init()){ printf(“初始化失败!\n”); } 否则{ 如果(!loadMedia()){ printf(“加载媒体失败!\n”); } 否则{ mainLoop(); } } close(); 返回0; } void mainLoop(){ int-quit=0; SDL_事件e; 而(!退出){ while(SDL_PollEvent(&e)!=0){ 如果(e.type==SDL\u退出){ 退出=1; } } SDL_BlitSurface(helloWorld,NULL,屏幕表面,NULL); SDL_更新内表面(窗口); } } int init(){ int成功=1; //初始化sdl if(SDL_Init(SDL_Init_视频| SDL_Init_事件)外部”C“/Cuth>”,则可以在C++中得到相同的问题行为。,c,gcc,sdl,sdl-2,C,Gcc,Sdl,Sdl 2,它与我合作如下(这是在Mac中) 现在实际代码是 #include "SDL.h" #include <stdio.h> int main(int argc, char* argv[]) { SDL_Window *window; // Declare a pointer // Create an application window with the following settings: window = SD

它与我合作如下(这是在Mac中)

现在实际代码是

#include "SDL.h"
#include <stdio.h>

int main(int argc, char* argv[]) {

    SDL_Window *window;                    // Declare a pointer


    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );


    // Check that the window was successfully made
    if (window == NULL) {
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    int quit = 0;
    SDL_Event e;

    while( !quit ){

        while( SDL_PollEvent( &e ) != 0 ){
            if(e.type == SDL_QUIT){
                quit = 1;
                printf("Closing the window ...\n");
            }
        }
    }

    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}

我发现你的问题非常有趣,当然也不明显

您的问题是由名为
close
的函数引起的。这是关闭文件描述符的函数的标准POSIX名称。它与系统功能相冲突。调用
SDL_Init
时,它连接到X服务器,查询一些值,然后断开连接(使用
XCloseDisplay
XCloseDisplay
,在其套接字描述符上调用
close
。问题是,您已经覆盖了系统
close
,而您的系统将被调用-这样不仅套接字保持未关闭状态,而且代码还会调用
SDL\u Quit
,从而停止任何进一步的SDL调用

使用C++链接,函数名被损坏(MULGLE对于函数重载等事情是必不可少的),因此它的最终名称将是“代码”>Z5CuteVe</C>,它不再与系统功能冲突。事实上,如果在函数声明之前添加“代码>外部”C“/Cuth>”,则可以在C++中得到相同的问题行为。 解决方法是重命名函数(最好不要使用标准名称),或者在其声明之前添加

static
说明符,这将限制其链接到当前翻译单元

链接器不报告多个定义冲突,因为
close
标记为弱符号(注意
W
前面):

$nm-D libc-2.19.so | grep close
# 
000da660 W关闭

问题代码没有错。您正在执行哪些操作以生成退出事件?请为您的问题制作一个完整的可编译的最小示例,并将其包含在问题中。单击窗口上的X按钮,则不会执行任何操作。此外,如果我添加其他的键或鼠标事件,它们在纯C中不工作,但是切换到C++编译器或源代码会导致所有SDL事件工作正常。如果你坚持不发布完整的例子,那么这个问题是无法回答的。抱歉我误读了你的问题。用完整的示例代码更新了我的第一篇文章。另外,我正在运行Ubuntu 14.04。你的回答帮了我的忙-谢谢!我们正在使用的教程是这样的:我不明白为什么我总是遇到seg故障,真不敢相信我没有想到这一点。facepalm(键盘)谢谢!
#include "SDL.h"
#include <stdio.h>

int main(int argc, char* argv[]) {

    SDL_Window *window;                    // Declare a pointer


    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );


    // Check that the window was successfully made
    if (window == NULL) {
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    int quit = 0;
    SDL_Event e;

    while( !quit ){

        while( SDL_PollEvent( &e ) != 0 ){
            if(e.type == SDL_QUIT){
                quit = 1;
                printf("Closing the window ...\n");
            }
        }
    }

    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}
$ gcc main.c -o test -I/Library/Frameworks/SDL2.framework/Headers  -framework SDL2 
$ ./test
Closing the window ...
$ nm -D libc-2.19.so | grep close
# <skiped>
000da660 W close