Windows Batch file for building SDL program

The following is a batch script I wrote which I use when compiling my C programs which use SDL on Windows 11. I want to share it and explain a little bit about how it works for those who develop on Windows and use GCC instead of Microsoft’s compiler included with Visual Studio.

SET DEVKIT=C:\Users\chand\scoop\apps\gcc\current

gcc -Wall -ansi -pedantic main.c -o main -I%DEVKIT%\include\SDL2 -L%DEVKIT%\lib -lmingw32 -lSDL2main -lSDL2

strip main.exe

main

The reason this script works is because I have gcc installed via scoop. My script sets an environment variable to the location where gcc is installed. It then uses this base folder as the path for the I and L flags in my command line to tell the compiler and linker where the include headers and linked libraries are.

The required files were extracted and copied directly into that folder and merged with the GCC installation. At the time the release I downloaded was “SDL2_mixer-devel-2.6.3-mingw.zip”.

The result is that with that small script, I can double click it and it will attempt to compile the source file named main.c and link it with the proper SDL files. I have used this method to get my games I made running on Windows. I also made a script for making a statically linked build as well.

SET DEVKIT=C:\Users\chand\scoop\apps\gcc\current

gcc -Wall -ansi -pedantic main.c -o main -I%DEVKIT%\include\SDL2 -Dmain=SDL_main -L%DEVKIT%\lib -lmingw32 -lSDL2main %DEVKIT%\lib\libSDL2.a -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid

strip main.exe

main

This may seem like basic information to experience users of SDL but I come from a Linux background and worked hard to set up a way to build my games under Windows so that others could play them. It was my creative way of building from the command line the same way I always do on Debian Linux. I don’t use an IDE because all of them confuse me.

Attached to this post is also my Tetris game which includes full source code, a pre-built executable, and these scripts that I included in my post. You can modify them according to your needs!

windows_sdl_chastetris_2.zip (833.5 KB)