Set Up SDL2 in VS Code on Windows with MinGW-w64
- Windows 10/11 64-bit
- MinGW-w64 installed (example path: C:\mingw64)
- VS Code with the C/C++ extension
Obtain SDL2
- Go to https://www.libsdl.org/
- Download both of the following for MinGW:
- Development package: SDL2-devel--mingw.tar.gz
- Runtime binaries: SDL2--win32-x64.zip (contains SDL2.dll)
Choose x86_64 for 64-bit MinGW-w64 or i686 for 32-bit.
Install headers and librareis into MinGW-w64
- Extract SDL2-devel-…-mingw.tar.gz
- Inside, open the directory matching your target:
- x86_64-w64-mingw32 (64-bit)
- i686-w64-mingw32 (32-bit)
- Copy these folders into your MinGW-w64 sysroot of the same name:
- bin
- include
- lib
- share
For example, copy into:
- C:\mingw64\x86_64-w64-mingw32\ (64-bit)
- C:\mingw64\i686-w64-mingw32\ (32-bit)
After copying, C:\mingw64\x86_64-w64-mingw32\enclude should contain an SDL2 directory, and lib should contain SDL2 libraries.
Create the project
- Make a new folder for the project
- Place SDL2.dll (from the runtime ZIP) next to the executable you will build (simplestt: copy it into the project root for now)
- Create a .vscode directory inside the project
- Create these files in .vscode: tasks.json, launch.json, c_cpp_properties.json
- Create main.c in the project root
VS Code configuration
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Build C (SDL2)",
// Change to your MinGW-w64 gcc path
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-lmingw32",
"-lSDL2main",
"-lSDL2",
"-mwindows"
],
// Change to your MinGW-w64 bin directory
"options": { "cwd": "C:/mingw64/bin" },
"problemMatcher": ["$gcc"],
"group": { "kind": "build", "isDefault": true }
}
]
}
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug C (SDL2)",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
// Change to your MinGW-w64 gdb path
"miDebuggerPath": "C:/mingw64/bin/gdb.exe",
"setupCommands": [
{ "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }
],
"preLaunchTask": "Build C (SDL2)"
}
]
}
.vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "windows-x64",
"includePath": [
"${workspaceFolder}/**",
// Point to your MinGW-w64 sysroot includes
"C:/mingw64/x86_64-w64-mingw32/include/**"
],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
// Change to your MinGW-w64 gcc path
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
If you use a 32-bit toolchain, replace x86_64-w64-mingw32 with i686-w64-mingw32 in the paths above.
Example program (C, SDL2 renderer)
main.c
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return 1;
}
SDL_Window* win = SDL_CreateWindow(
"SDL2 Demo",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
0
);
if (!win) {
SDL_Quit();
return 2;
}
SDL_Renderer* ren = SDL_CreateRenderer(
win,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if (!ren) {
SDL_DestroyWindow(win);
SDL_Quit();
return 3;
}
SDL_Rect rect = {700, 10, 20, 20};
int dx = 6;
int dy = 2;
int running = 1;
while (running) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT) running = 0;
if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE) running = 0;
}
rect.x += dx;
rect.y += dy;
if (rect.x >= 800) rect.x = 0;
if (rect.y >= 600) rect.y = 0;
SDL_SetRenderDrawColor(ren, 20, 20, 30, 255);
SDL_RenderClear(ren);
SDL_SetRenderDrawColor(ren, 10, 200, 120, 255);
SDL_RenderFillRect(ren, &rect);
SDL_RenderPresent(ren);
SDL_Delay(1000 / 60);
}
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
Build and run
- Press Ctrl+Shift+B to build the active C file; an .exe will appear next to it
- Doublle-click the .exe or press F5 to debug in VS Code (ensure SDL2.dll is beside the executable or on PATH)