Skip to content

Instantly share code, notes, and snippets.

@martinmoene
Created September 23, 2024 19:46
Show Gist options
  • Save martinmoene/c14d05e401910a69cd6832f36a85605a to your computer and use it in GitHub Desktop.
Save martinmoene/c14d05e401910a69cd6832f36a85605a to your computer and use it in GitHub Desktop.
Batch script to return MSVC product code
::
:: Subroutine CompilerVersion( version ) - Return product code: 8, 10... 16, 17.
::
:CompilerVersion version
@echo off & setlocal enableextensions
set tmpprogram=_getcompilerversion.tmp
set tmpsource=%tmpprogram%.c
:: Compiler versions, https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B
::
:: version code / _MSC_VER (Product name)
:: VC 8 / 1500 (VS2008)
:: VC 10 / 1600 (VS2010)
:: VC 11 / 1700 (VS2012)
:: VC 12 / 1800 (VS2013)
:: VC 14 / 190x (VS2015)
:: VC 15 / 191x (VS2017)
:: VC 16 / 192x (VS2019)
:: VC 17 / 193x (VS2019)
::
echo #include ^<stdio.h^> >%tmpsource%
echo int main(){printf("%%d\n", _MSC_VER / 100 - 5 - (_MSC_VER ^< 1900) + (_MSC_VER / 10) %% 10);} >>%tmpsource%
cl /nologo %tmpsource% >nul
for /f %%x in ('%tmpprogram%') do set version=%%x
del %tmpprogram%.* >nul
endlocal & set %1=%version%& goto :EOF
rem
rem end of file
rem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment