Hello! I made this document for anyone trying to setup an enviroment for SDL2 on Windows using VS Code for building and debugging.
This is just a basic setup to test everything works and compiles correctly. This is adapted from this guide by Adam Richardson, made to work with the UCRT enviroment.
So, let's get to it.
You can download from here: https://www.msys2.org/ Then just follow the instructions. Provided on the official website for instalation.
After the installation is complete, open Windows Terminal, (to add MSYS2 profiles to Windows Terminal you can read this), and then update your packages with this command.
pacman -Syuu
Repeat a few times until everything is up to date, note that your terminal might restart (or straight up close) during this process. Just open it up again and execute the command again to make sure everything is up to date.
Run the following command on the UCRT shell:
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain mingw-w64-ucrt-x86_64-SDL2 mingw-w64-ucrt-x86_64-SDL2_image mingw-w64-ucrt-x86_64-SDL2_sound mingw-w64-ucrt-x86_64-SDL2_mixer mingw-w64-ucrt-x86_64-SDL2_gfx mingw-w64-ucrt-x86_64-SDL2_net mingw-w64-ucrt-x86_64-SDL2_ttf mingw-w64-ucrt-x86_64-cmake make
This will all the packages you might need for building C/C++ programs, and the SDL2 base library and extension libraries you might need for development.
Open up an MSYS2 session using the UCRT environment, and execute the following command:
$ gcc --version
gcc.exe (Rev2, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Note that here the output is included.
You can do the same with, g++ --version
and gdb --version
, to check if they are working, you should get similar results.
For VS Code you need to have the C/C++ Extension installed. After everything is setup, you can create a directory, then create a new file called hello.c
and open it in VS Code, then hit Ctrl+Shift+P
to open the command palette, and search for edit configurations (UI), there you can select the path to your compiler, and create a configuration for SDL, you can also create one using JSON like this:
...
{
"name": "SDL2",
"includePath": [
"${workspaceFolder}/**",
"C:\\msys64\\ucrt64\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64",
"compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
"compilerArgs": [
"-IC:/msys64/ucrt64/bin/../include",
"-IC:/msys64/ucrt64/bin/../include/SDL2",
"-Dmain=SDL_main",
"-LC:/msys64/ucrt64/bin/../lib",
"-lmingw32",
"-mwindows",
"-lSDL2main",
"-lSDL2"
]
}
As you can see, my config has a compilerArgs property, if you are compiling straight from the MSYS2 terminal, you can just use
For C:
gcc hello.c -o hello $(pkg-config --cflags --libs sdl2)
That last bit, executes the pkg-config command and appends everything your linker needs to know what libraries you want to use. The process for VS Code though, it's a bit more manual.
In a terminal execute pkg-config --cflags --libs sdl2
You should see an output similar to this:
-IC:/msys64/ucrt64/bin/../include -IC:/msys64/ucrt64/bin/../include/SDL2 -Dmain=SDL_main -LC:/msys64/ucrt64/bin/../lib -lmingw32 -mwindows -lSDL2main -lSDL2
Just copy it, and use it for your compiler args, separating each argument as shown above.
Then paste this into "hello.c"
#include <SDL2/SDL.h>
#include <stdio.h>
const int WIN_WIDTH = 800, WIN_HEIGHT = 600;
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
// Video Initialization
SDL_InitSubSystem(SDL_INIT_VIDEO);
if (!SDL_WasInit(SDL_INIT_VIDEO))
{
printf("SDL video was not initialized. SDL_Error: %s\n", SDL_GetError());
return -1;
}
printf("Video was initialized succesfully!\n");
window = SDL_CreateWindow(
"SDL2 Window", // Window label
SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, // Position of the window in screen.
WIN_WIDTH, WIN_HEIGHT,
0 // Extra flags, defaults SDL_WINDOW_SHOWN to true if 0
);
if (!window)
{
printf("SDL window was not created. SDL_Error: %s\n", SDL_GetError());
return -1;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
{
printf("SDL renderer was not created. SDL_Error: %s\n", SDL_GetError());;
return -1;
}
// rendering steps
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer); // fills the screen witn draw color
SDL_RenderPresent(renderer); // updates the screen
SDL_Event event;
while (1)
{
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
break;
}
}
// Clear everything before quiting.
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
This code just initializes SDL and creates a window with a white background.
Now you should be able to configure a build task like this one
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/ucrt64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-IC:/msys64/ucrt64/bin/../include",
"-IC:/msys64/ucrt64/bin/../include/SDL2",
"-Dmain=SDL_main",
"-LC:/msys64/ucrt64/bin/../lib",
"-lmingw32",
"-mwindows",
"-lSDL2main",
"-lSDL2"
],
"options": {
"cwd": "C:/msys64/ucrt64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
And that's it, you should now be able to build and run and debug your project from within VS Code!
Thanks for reading!