Skip to content

Instantly share code, notes, and snippets.

@DennyLindberg
Last active August 18, 2024 02:22
Show Gist options
  • Save DennyLindberg/8c0a5e7471a30e6868a00757e8483b78 to your computer and use it in GitHub Desktop.
Save DennyLindberg/8c0a5e7471a30e6868a00757e8483b78 to your computer and use it in GitHub Desktop.
A pseudo-make for building a C application using MSVC
::::
:: A pseudo-make for building a C application using MSVC.
:: https://github.com/DennyLindberg
::
:: Use any text editor and commandline. RemedyBG or RAD Debugger for debugging.
:: https://remedybg.itch.io/remedybg
:: https://github.com/EpicGamesExt/raddebugger
::
:: Example:
:: ./make build run arg1 arg2 arg3
:: ./make build debug arg1 arg2 arg3
::
:: Usage:
:: Put this bat file in the root of your project folder.
:: Modify PROJECT CONFIGURATION in this file to your liking.
::
:: Commandline:
:: ./make build build exe
:: ./make run start exe
:: ./make clean delete all output files
:: ./make build run build then run exe if build was successful
::
:: Debugging:
:: ./make dbuild build exe with debug symbols (pdb)
:: ./make debug debug exe with default debugger
:: ./make remedybg debug exe with RemedyBG
:: ./make raddbg debug exe with RAD Debugger
:: ./make dbuild debug build with pdb then debug with default debugger
:: ./make dbuild remedybg same for RemedyBG
:: ./make dbuild raddbg same for RAD Debugger
::
:: You can pass a maximum of 9 extra arguments to your exe. Note that there are limitations to argument formatting.
:: ./make build run arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9
:: e.g. ./make build run width:320 height:240
::
:: Requires cl.exe from MSVC in your path. You have three choices:
:: 1) Easiest: Install a portable version of MSVC using PortableBuildTools
:: https://github.com/Data-Oriented-House/PortableBuildTools
:: cl.exe should now be available in all commandlines/terminals on your PC
::
:: 2) Inconvenient: Run this bat file from the 'Developer Command Prompt for VS 2017' or newer
:: Find it in the start menu
::
:: 3) Annoying: Run vcvarsall.bat before running this bat file.
:: You are on your own
::
::::
@echo off
:::: PROJECT CONFIGURATION
set exe_file=main.exe
set src_files=src/*.c
set includes_path=include/
set libs=lib/*.lib
set compiler=/Wall /wd5045 /std:c17
:: /wd5045 disables Spectre warnings
::set hide_console=/SUBSYSTEM:WINDOWS
:: Uncomment this line to hide the console. Warning, this will disable output to any commandlines.
set linker=/link /ENTRY:mainCRTStartup %hide_console%
:: /ENTRY:mainCRTStartup uses void main(void) instead of WinMain
set includes=/external:W3 /external:I %includes_path%
:: /external:W3 silences warning spam from libraries like SDL2 when using /Wall
:: DEBUGGER SETTINGS
:: :raddbg or :remedybg
set debugger=:raddbg
set debug_raddbg_path=C:\raddbg.exe
set debug_remedybg_path=C:\remedybg.exe
set debug_entry_file=src/main.c
set debug_settings=/Z7
:: /Z7 generates the PDB file (other options /Zi and /ZI)
:::: COMMAND SWITCH
:: attempt goto using first and second parameter for calls like "./make build" or "./make build run"
:: The remaining commands are stored in program_args (maximum 9 args).
set command1=%1
set command2=%2
SHIFT
set program_args=%1 %2 %3 %4 %5 %6 %7 %8 %9
if "%command1%" == "" goto build
goto :%command1% 2>nul || (
echo invalid make command
goto :eof
)
:secondparam
SHIFT
set program_args=%1 %2 %3 %4 %5 %6 %7 %8 %9
goto :%command2% 2>nul || (
goto :eof
)
:::: "MAKEFILE" COMMANDS
:dbuild
set build_debug=%debug_settings%
set linker_debug=/DEBUG
goto build
goto :eof
:build
cl.exe /Fe:%exe_file% %compiler% %build_debug% %includes% %src_files% %linker% %linker_debug% %libs% || exit /b 1
goto secondparam
goto :eof
:run
%exe_file% %program_args%
goto :eof
:clean
del *.exe *.obj *.pdb *.ilk 2>nul
goto :eof
:debug
goto %debugger%
goto :eof
:remedybg
start %debug_remedybg_path% -g -q %cd%/%exe_file% %program_args%
timeout /t 1
%debug_remedybg_path% open-file %cd%/%debug_entry_file%
goto :eof
:raddbg
start %debug_raddbg_path% --auto_run %cd%/%exe_file% %program_args%
goto :eof
@DennyLindberg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment