Skip to content

Instantly share code, notes, and snippets.

Last active August 14, 2023 07:36
Show Gist options
  • Save fschr/92958222e35a823e738bb181fe045274 to your computer and use it in GitHub Desktop.
Save fschr/92958222e35a823e738bb181fe045274 to your computer and use it in GitHub Desktop.
SDL2 Hello World | SDL2 Getting Started | SDL | OpenGL
// SDL2 Hello, World!
// This should display a white screen for 2 seconds
// compile with: clang++ main.cpp -o hello_sdl2 -lSDL2
// run with: ./hello_sdl2
#include <SDL2/SDL.h>
#include <stdio.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int main(int argc, char* args[]) {
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
return 1;
window = SDL_CreateWindow(
if (window == NULL) {
fprintf(stderr, "could not create window: %s\n", SDL_GetError());
return 1;
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
return 0;
Copy link

ashkan-o commented May 1, 2021

please rename gistfile1.txt to main.cpp

Copy link

fschr commented May 1, 2021

@AshkanLaei done. If you have trouble running this, I can probably help.

Copy link

eabase commented May 15, 2021

Which dependencies are needed to install and to where?

  • clang?
  • SDL?

Maybe provide link to the downloads we need for out-of-the-box experience?

Copy link

@eabase do you use Linux or Windows?

if in linux just type:

sudo apt install libsdl2-dev build-essentials

if in windows you need to download from, and edit your Makefile to include libSDL (it's a long way)

Copy link

pedro15 commented Jun 9, 2021

i only get and the program is not executable
Fatal Error: Out of memory - aborting

Copy link

i had it running, but just a note for windows users:

Fixing the above, just use something like gcc hello.c -o hello -I./include -L./lib -lSDL2 (or whatever)

Copy link

eabase commented Jul 1, 2021

I have since provided a much more extensive PoC for using SDL2, here.

Copy link

i managed to run it by running the following commands
g++ c.cpp -o c -lSDL2main -lSDL2&& ./c
anyone need help contact me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment