Skip to content

Instantly share code, notes, and snippets.

@BeErikk
Last active March 20, 2026 11:45
Show Gist options
  • Select an option

  • Save BeErikk/786df70f63c578a8acfeb75c0da60e64 to your computer and use it in GitHub Desktop.

Select an option

Save BeErikk/786df70f63c578a8acfeb75c0da60e64 to your computer and use it in GitHub Desktop.
Windows console script to enable the MS compiler with custom environment settings
@echo off
::@title MS C++ compiler
:: Windows console script to enable the MS compiler with custom environment settings
:: Jerker Bäck 2017
:: delims:vv vvv vv v
:: line : PlatformIdentity = "UAP, Version=10.0.19041.0"
:: tokens: ^1 ^2 ^3 ^4
setlocal
::set WSL="C:\Windows\System32\wsl.exe"
set vswhereprog="C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
%vswhereprog% -latest -property installationPath | wsl.exe sed 's:\\:/:g' | wsl.exe tr -d '\r\n' > %TEMP%\vspath.tmp
endlocal
set /p vsinstalldir=<%TEMP%\vspath.tmp
set /p vctoolsversion=<"%vsinstalldir%/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt"
echo %vctoolsversion%|wsl.exe tr -d '\r\n' > %TEMP%\vspath.tmp
set /p vctoolsversion=<%TEMP%\vspath.tmp
set vctools=%vsinstalldir%/VC/Tools/MSVC/%vctoolsversion%/
reg.exe query "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots" -f KitsRoot10 | wsl.exe sed -n 's:.*REG_SZ *\([[:print:]]*\).*:\1:p' | wsl.exe sed 's:\\:/:g' | wsl.exe tr -d '\r\n' > %TEMP%\vspath.tmp
set /p kitsroot10=<%TEMP%\vspath.tmp
for /f "tokens=4 delims=^=," %%i in ('find.exe "PlatformIdentity" ^< "%kitsroot10%SDKManifest.xml"') do set kitsversion=%%i
set kitsversion=%kitsversion:"=%
echo %kitsversion%|wsl.exe tr -d ' \r\n' > %TEMP%\vspath.tmp
set /p kitsversion=<%TEMP%\vspath.tmp
del %TEMP%\vspath.tmp
set userlib=F:/libraries/
@echo.
echo Using MSVCtools version: %vctoolsversion%
echo Using Windows SDK version: %kitsversion%
@echo.
set INCLUDE=%vctools%include;%vctools%atlmfc/include;%kitsroot10%Include/%kitsversion%/ucrt;%kitsroot10%Include/%kitsversion%/um;%kitsroot10%Include/%kitsversion%/shared;%kitsroot10%Include/%kitsversion%/km;%kitsroot10%Include/%kitsversion%/winrt;%kitsroot10%Include/%kitsversion%/cppwinrt;%vsinstalldir%/VC/Auxiliary/VS/include;%vsinstalldir%/VC/Auxiliary/VS/UnitTest/include;%userlib%include;%userlib%include/ntnative;%userlib%include/wtl;
set LIB64=%vctools%lib/x64;%vctools%atlmfc/lib/x64;%kitsroot10%Lib/%kitsversion%/ucrt/x64;%kitsroot10%Lib/%kitsversion%/um/x64;%kitsroot10%Lib/%kitsversion%/km/x64;%vsinstalldir%/VC/Auxiliary/VS/UnitTest/lib/x64;%userlib%lib/x64;
set LIB32=%vctools%lib/x86;%vctools%atlmfc/lib/x86;%kitsroot10%Lib/%kitsversion%/ucrt/x86;%kitsroot10%Lib/%kitsversion%/um/x86;%kitsroot10%Lib/%kitsversion%/km/x86;%vsinstalldir%/VC/Auxiliary/VS/UnitTest/lib/x86;%userlib%lib/x86;
set PATHVS=%vsinstalldir%/Common7/IDE;%vsinstalldir%/Common7/Tools;%vsinstalldir%/Common7/IDE/VC/VCPackages;%vsinstalldir%/Common7/IDE/CommonExtensions/Microsoft/TestWindow;
set PATH64=%vctools%bin/HostX64/x64;%kitsroot10%bin/%kitsversion%/x64;%kitsroot10%bin/x64;
set PATH32=%vctools%bin/Hostx86/x86;%kitsroot10%bin/%kitsversion%/x86;%kitsroot10%bin/x86;
set orgPATH=%PATH%
if /i "%1"=="x86" ( call :32bitenviron ) else ( call :64bitenviron )
if errorlevel 1 goto Failed
set LIBPATH=%LIB%
goto :eof
:: -----------------------------------------------------------------------------
:64bitenviron
@title MS C++ compiler 64-bit environment
echo Using 64-bit environment - use "mscompiler x86" for 32-bit
set LIB=%LIB64%
set PATH=%PATH64%;%PATHVS%;%PATH%
set _CL_=-link -subsystem:console -machine:x64
@exit /B 0
:: -----------------------------------------------------------------------------
:32bitenviron
@title MS C++ compiler 32-bit environment
echo Using 32-bit environment
set LIB=%LIB32%
set PATH=%PATH32%;%PATHVS%;%PATH%
set _CL_=-link -subsystem:console -machine:x86
@exit /B 0
:: -----------------------------------------------------------------------------
:Failed
@echo.
@echo ===============================================================================
@echo.
@echo Error: compiler script failed
@echo.
@echo ===============================================================================
@echo.
@BeErikk
Copy link
Copy Markdown
Author

BeErikk commented Apr 24, 2021

This script allows a stripped-down custom environment for the MS compiler tools (vs the Visual Studio prompt).
It's not very fast but faster than Visual Studio prompt.

There are imo two cumbersome settings in current Visual Studio installations:

  • %vsinstalldir%/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt
  • %kitsroot10%SDKManifest.xml

which gives the necessary compiler version and SDK tools version numbers.

In the script, I use wsl.exe sed and find.exe to get the numbers and wsl.exe tr to strip CRLN
So it has dependencies of WSL. I tried several alternatives but this is one way to do it.

Another way is to symlink the compiler to say C:\develop\current\vctools and manually update the symlink at each Visual Studio update. This has the advantage of giving a short path without spaces, but is of course less elegant. (I do both). As a side note MS really could have registered these two numbers somewhere more script friendly.

I am, of course, using WSL1.

@BeErikk
Copy link
Copy Markdown
Author

BeErikk commented Mar 20, 2026

JFYI: F:/libraries/ is the place i keep track of my external libraries, typically by symlinks to F:/libraries/include
Example Microsoft WIL:
Path to git repo: F:\libraries\ui\wil\wil
symlink: mklink /D "F:\libraries\include\wil" "..\ui\wil\wil\include\wil"

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