SDL2 can not show window in Ubuntu 22.04

my code :

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

int main(int argc, char* argv[]) 
{
    if (SDL_Init(SDL_INIT_VIDEO)  != 0)
    {
        printf("Error: %s\n", SDL_GetError());
        return -1;
    }

    SDL_Window *window = SDL_CreateWindow("My", 
    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
      640, 480, SDL_WINDOW_SHOWN);

    if (!window)
    {
        printf("Error: %s\n", SDL_GetError());
        return -1;
    }

     SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); // 创建渲染器
    if (renderer == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Couldn't create renderer: %s", SDL_GetError());
        return 1;
    }
  

    //Set colour of renderer
    SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 );

    //Clear the screen to the set colour
    SDL_RenderClear( renderer );

    //Show all the has been done behind the scenes
    SDL_RenderPresent( renderer );

     SDL_Delay(10000);
  
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

My program has been successfully compiled, but i can not see any window in Ubuntu 22.04 when i run, There are no errors, who can tell me why ?

On Ubuntu I tend to have to use #include <SDL2/SDL.h>, but if your program is compiling then you’ve got your PATH variable modified to fix that (or the SDL headers are in your project with a local install).
Are there any error messages in your console?
What does your compiler line look like?
If you have a local install, does your compiler also link to the local binaries?

My program has been successfully compiled, There are no errors ,but no window show !

You are not polling for events (if only SDL_QuitEvent). On some platforms that is necessary for the window to appear.

1 Like