How to setup SDL2 with CMake,mingw gcc compiler in Visual Studio Code?

So I wanted to know how I can setup SDL2 in Visual studio Code. So in my project I want want to have include,src and res folder and setup cmake for debugging and running with GCC compiler. Hope someone can help me with this. Thanks :smiley:

I think the most simple way is:

  1. Install MSYS2 from https://www.msys2.org
  2. Open “MSYS2 MinGW 64-bit” terminal then install needed software and libraries:
    pacman -Syu to update repositories and upgrade outdated packages
    pacman -S --needed mingw-w64-x86_64-toolchain mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_image mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_ttf mingw-w64-x86_64-cmake
    You can install other needed packages, if you need.
  3. Add “MSYS2 MinGW 64-bit” terminal to your VSCode by editing settings.json:
    Open command palette by CTRL + SHIFT + P
    Choose “Open user settings (JSON)”
    Add a new subobject to terminal.integrated.profiles.windows object:
"MSYS2 MinGW 64-bit": {
	"path": "C:\\msys64\\msys2_shell.cmd",
	"args": [
		"-defterm",
		"-here",
		"-no-start",
		"-mingw64"
	]
}

Adjust paths if needed.
Your config will look like this:

{
	// many other options
	"terminal.integrated.profiles.windows": {
		"MSYS2 MinGW 64-bit": {
			"path": "C:\\msys64\\msys2_shell.cmd",
			"args": [
				"-defterm",
				"-here",
				"-no-start",
				"-mingw64"
			]
		},
		// many other terminals
	}
}
  1. Install and Microsoft C++ extension via extension marketplace, and you may need to install “CMake” and “CMake Tools” extensions, if you want to use CMake.
    Optional: You can also use clangd as code model, you need to install clangd extension and package ( mingw-w64-clang-x86_64-clang-tools-extra) then provide path to clangd executable in VSCode:
    C:\msys64\mingw64\bin\clangd.exe

  2. In VSCode settings find “Cmake: Cmake Path” setting and enter path to your MinGW CMake:
    C:\msys64\mingw64\bin\cmake.exe

  3. Create/open your CMake project and start exploring it by yourself.

Maybe I missed something, so feel free to ask about it.

You also can just use Makefiles but it’s not simple for beginner, and you will face some code model issues in VSCode.

1 Like

I already downloaded the mingw64 already and placed it in my c directory, also downloaded the SDL2_image and SDL2 64 bit version.

.Why do we need to use MSYS2 ?

MSYS2 is not required, but provides easy way to install and update packages.
Without MSYS2 steps are same but instead of packages you need to install all stuff by yourself.
Also this way will require some special configuration, like providing toolchain paths to CMake.

Any tips to learn to set up projects for SDL in general? Yt Video? Or learn something?

You can check this template for reference.
For best experience I think you need to start from CMake, quick search shows that YT have many videos about SDL + CMake.

also, i would like to ask about the best practice of creating an SDL2 Project/ Folder structure in general!

Basically that depends on the project itself. And sometimes this is about the style/convention.
For now there’s no need to be complicated, keep things simple:

Project/
├── CMakeLists.txt
└── src/
    ├── game.cpp
    ├── game.h
    └── main.cpp

I think the best practice is to use whatever suits your needs in this case.

Thanks for the reply, actually i m trying to create a small 2D game engine with SD2. Also do you have discord if I can have it for asking some doubts :sweat_smile:

Sorry i don’t use it