A guide for setting up SDL2 with C/C++ on Windows 11 using MSYS2 and VS Code.
This tutorial assumes that VS Code is already installed.
Make sure to install the C/C++ extension.
Installing MSYS2
-
Visit msys2.org and download the installer.
-
After running the installer, follow the steps to install MSYS2 and leave everything as default.
-
Once the installation is finished, MSYS2 will run the UCRT64 environment.
-
Ensure everything is up to date by typing the following command:
pacman -SuyMore information about updating MSYS2 can be found on this page.
-
Close out of the UCRT64 environment and open the MINGW64 environment by using the windows search bar.
Installing necessary packages
-
Run the following command to install gcc/g++, along with some other necessary tools like make.
pacman -S --needed base-devel mingw-w64-x86_64-toolchain -
When asked to "Enter a selection (default=all)", press enter to accept the default.
-
Once the install finishes, verify that you have gcc and g++, by running the following:
gcc --versiong++ --version -
Optionally, run
pacman -Suyagain to check for updates.
Setting up Environment Variables
- In the windows search bar, search for
Edit environment variables for your accountand open it. - Click on path, then click edit.
- Click new, and add this to the new path:
C:\msys64\mingw64\bin - Click OK, then OK again to exit out.
Setting up the workspace
-
Navigate to the msys2 directory using the file explorer, which is typically located at:
C:\msys64 -
Enter the home directory and enter your user folder.
-
Create a new folder in this location and name whatever you like, such as sdl2.
-
The final path to your sdl2 workspace should be:
C:\msys64\home\USER\sdl2 -
Verify that you can access this folder through the MSYS2 MINGW64 terminal by typing
lsthencd sdl2 -
You can create a new folder in this directory which will hold all the files for a project.
(Optional) Adding MSYS2 Terminal to VS Code
- Open
Preferences: Open User Settings (JSON)using the command palette in VS Code. (Ctrl + Shift + P on windows) - Add the following code to the JSON file (inside the curley brackets):
"terminal.integrated.profiles.windows":
{
"MSYS2":
{
"path": "C:\\msys64\\usr\\bin\\bash.exe",
"args":["--login","-i"],
"env":
{
"MSYSTEM": "MINGW64",
"CHERE_INVOKING": "1"
}
}
},
"terminal.integrated.defaultProfile.windows": "MSYS2"
- Remove the last line if you don't want MSYS2 as the default terminal.
- This code assumes MSYS2 is installed on the default path.
The next step is to set up the SDL2 header/library files.
- Visit the website for SDL2 and under Download, click SDL Releases.
- Click on the file named "SDL2-devel-2.28.5-mingw.zip". The version number might be slightly different.
- Extract the file.
- Inside the SDL2-2.28.5 folder, you should see two different folders containing mingw in the name.
- i686-w64-mingw32 contains 32-bit libraries and x86_64-w64-mingw32 contains 64-bit libraries. I use the 64-bit version.
- Inside these folders is an include and lib folder. There is also a bin folder that contains SDL2.dll.
- Create a new folder named
sdl2devand drag the include and lib folder into it. - For every new project, include the
sdl2devfolder and the SDL2.dll file (inside the bin folder) in the base directory. - For simplicity, you can move these files into another folder and copy/paste for any new project.
The final project workspace should look something like this:
C:\msys64\home\USER\sdl2\PROJECTNAME should contain the sdl2dev folder, which contains the include and lib folders.
C:\msys64\home\USER\sdl2\PROJECTNAME should also contain SDL2.dll outside of the sdl2dev folder.
Again, make sure that your project contains the include/lib folders and SDL2.dll. These are important for compiling.
Extra Notes:
There is a way to circumvent adding the include/lib folders and SDL2.dll everytime you start a new project, but it requires messing with VS Code .json files.
Whenever you compile a program with SDL2, you need to include the paths to the SDL2 libraries and header files. An example is shown below.
To compile a program, you have a few options. The first is to run a command on the terminal. The second is to use a Makefile.
Both of the below commands work for compiling a program. If your include/library paths are different, make sure to set it correctly.
g++ main.cpp -o main -I ./sdl2dev/include/SDL2 -L ./sdl2dev/lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2
g++ main.cpp -o main -IC:./sdl2dev/include/SDL2 -LC:./sdl2dev/lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2
You can also set up a Makefile to this process easier, especially if your project includes a lot of files.
Additional Notes
You can use -IC: or -I to set the include path for header files. The same goes for library files, with -LC: or -L. Note that -IC:/-LC: doesn't need a space before the include path.
I'm using ./ to start the include path from the folder I'm compiling in. If something doesn't work for you, try using the full path to the include/lib files.
-w -Wl,-subsystem,windows are used to suppress warnings and remove a console window.
-lmingw32 -lSDL2main -lSDL2 are some additional flags for linking to SDL2.