Skip to content

Instantly share code, notes, and snippets.

@thales17
Last active June 9, 2026 22:19
Show Gist options
  • Select an option

  • Save thales17/fb2e4cff60890a51d9dddd4c6e832ad2 to your computer and use it in GitHub Desktop.

Select an option

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`
@s10g

s10g commented Mar 11, 2020

Copy link
Copy Markdown

Thanks bro

@jmgbsas

jmgbsas commented Dec 22, 2020

Copy link
Copy Markdown

I believe sould be gcc -o hello -Wall hello.c -l "sdl2-config -cflags -libs"
but I get error too ...
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot fi
nd -lsdl2-config -cflags -libs
collect2.exe: error: ld returned 1 exit status
I willsearch for sdl2-config
Thanks
(windows MSYS2 msys64/mingw64 and 32)
I had succefuly compilation with:
gcc hello.c -o hello $(pkg-config --cflags --libs sdl2)
or for c++
g++ sdltest.cpp -o outsdl $(pkg-config --cflags --libs sdl2)

@s10g

s10g commented Dec 23, 2020

Copy link
Copy Markdown

sdl2-config is a command

The stuff between `` (like sdl2-config --cflags --libs) returns on stdin the output from running the command `sdl2-config --cflags --libs`.

$ sdl2-config
Usage: /mingw64/bin/sdl2-config [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--cflags] [--libs] [--static-libs]

As you can see here, sdl2-config is merely an SDL2 provided command to make it easier to fill in the compiler options:

$ sdl2-config --cflags --libs
-I/mingw64/include/SDL2 -Dmain=SDL_main
-L/mingw64/lib -lmingw32 -lSDL2main -lSDL2 -mwindows

So the reason you're getting an error is.... oh I see you already edited it 👍

@jmgbsas

jmgbsas commented Dec 24, 2020

Copy link
Copy Markdown

ok sdl2-config is a command .... for that reson is the character ` ...I believed was an option and changed for " , I will see
Thanks, happy New Year !

@denjhang

denjhang commented May 5, 2021

Copy link
Copy Markdown

I installed sdl2 in msys2 using the above command, but "Could NOT find SDL2" still appears when I use cmake. Can you help me see what is going on?

@arturkittel

Copy link
Copy Markdown

If somebody have troubles with 'sdl2-config --cflags --libs', run whole command from MSYS2

$ sdl2-config --cflags returns
-IC:/msys64/mingw64/include/SDL2 -Dmain=SDL_main

sdl2-config --libs returns
-LC:/msys64/mingw64/lib -lSDL2main -lmingw32 -lSDL2main -lSDL2 -mwindows

@HappyGoFishing

Copy link
Copy Markdown

Thanks for this!

@rrumrum

rrumrum commented Mar 7, 2023

Copy link
Copy Markdown

Each time I install something for programming projects I get slightly closer to understanding what on earth is going on. Thank you for this page.

@devashish234073

Copy link
Copy Markdown

I am getting the following error :
C:/M/B/src/mingw-w64/mingw-w64-crt/crt/ucrtexewin.c:67: undefined reference to `wWinMain'

My vscode's tasks.json is :
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-I C:\\msys64\\mingw64\\include\\SDL2", "-L C:\\msys64\\mingw64\\lib", "-lSDL2main", "-lSDL2", "-mwindows", "-municode" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }

@devashish234073

devashish234073 commented Jul 19, 2023

Copy link
Copy Markdown

My issue is fixed I had to replace int main with "int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow ){"

               And had to change the order of arguments:

Here's my final tasks.json:

{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-I C:\\msys64\\mingw64\\include\\SDL2", "-L C:\\msys64\\mingw64\\lib", "-lmingw32", "-lSDL2main", "-lSDL2", "-lSDL2_image", "-mwindows" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }

and this is my final code:

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

const int WIDTH = 800, HEIGHT = 600;

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow ){
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;
}`

@Algiuxs

Algiuxs commented Dec 6, 2023

Copy link
Copy Markdown

My issue is fixed I had to replace int main with "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){"

               And had to change the order of arguments:

Here's my final tasks.json:

{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-I C:\\msys64\\mingw64\\include\\SDL2", "-L C:\\msys64\\mingw64\\lib", "-lmingw32", "-lSDL2main", "-lSDL2", "-lSDL2_image", "-mwindows" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }

and this is my final code:

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

const int WIDTH = 800, HEIGHT = 600;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){ 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; }`

solved my problem, thank you

@neel-bp

neel-bp commented Aug 14, 2024

Copy link
Copy Markdown

big thanks friend

@qingzhou-2

Copy link
Copy Markdown

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