Skip to content

Instantly share code, notes, and snippets.

@thales17
Last active April 14, 2024 07:20
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`
@s10g
Copy link

s10g commented Mar 11, 2020

Thanks bro

@jmgbsas
Copy link

jmgbsas commented Dec 22, 2020

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
Copy link

s10g commented Dec 23, 2020

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
Copy link

jmgbsas commented Dec 24, 2020

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
Copy link

denjhang commented May 5, 2021

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?

@makomako84
Copy link

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

Thanks for this!

@rrumrum
Copy link

rrumrum commented Mar 7, 2023

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

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
Copy link

devashish234073 commented Jul 19, 2023

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
Copy link

Algiuxs commented Dec 6, 2023

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

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