Skip to content

Instantly share code, notes, and snippets.

@thales17
Last active March 31, 2025 04:16
Show Gist options
  • Save thales17/fb2e4cff60890a51d9dddd4c6e832ad2 to your computer and use it in GitHub Desktop.
Save thales17/fb2e4cff60890a51d9dddd4c6e832ad2 to your computer and use it in GitHub Desktop.
msys2 sdl2 setup

Download and install msys2 64bit

Update msys2

  • Update msys2 64bit after install by running pacman -Syu if pacman needs to be updated you might have to close and reopen the terminal and run pacman -Syu again

Install build tools

  • pacman -S git mingw-w64-x86_64-toolchain mingw64/mingw-w64-x86_64-SDL2 mingw64/mingw-w64-x86_64-SDL2_mixer mingw64/mingw-w64-x86_64-SDL2_image mingw64/mingw-w64-x86_64-SDL2_ttf mingw64/mingw-w64-x86_64-SDL2_net mingw64/mingw-w64-x86_64-cmake make

Compile Hello World

hello.c

#include <stdio.h>
#include <SDL2/SDL.h>

const int WIDTH = 800, HEIGHT = 600;

int main(int argc, char *argv[]) {
  SDL_Window *window;
  SDL_Renderer *renderer;
  if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    printf("SDL_Init failed: %s\n", SDL_GetError());
    return 1;
  }

  window = SDL_CreateWindow("Hello, World!",
                                        SDL_WINDOWPOS_UNDEFINED,
                                        SDL_WINDOWPOS_UNDEFINED,
                                        WIDTH, HEIGHT,
                                        SDL_WINDOW_ALLOW_HIGHDPI);
  if(window == NULL) {
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
  }
  
  renderer = SDL_CreateRenderer(window, -1, 0);
  SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  SDL_RenderClear(renderer);

  SDL_RenderPresent(renderer);
  
  SDL_Event event;
  while(1) {
    if(SDL_PollEvent(&event)) {
      if(event.type == SDL_QUIT) {
        break;
      }
    }
  }

  SDL_DestroyWindow(window);

  SDL_Quit();
  return 0;
}

Compiling with mingw64

gcc -o hello -Wall hello.c `sdl2-config --cflags --libs`
@neel-bp
Copy link

neel-bp commented Aug 14, 2024

big thanks friend

@qingzhou-2
Copy link

I want to compile ffmpeg-n4.4.1 with msys2 mingw64, it's request sdl version within "sdl2 >= 2.0.1 sdl2 < 2.1.0",
but the cmd pacman -Ss sdl2, show that this sdl2 version is mingw64/mingw-w64-x86_64-SDL2 2.32.2-1 [installed]
I cant find the correct version of SDL2, can you help me how to do? thank you very much.

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