Don’t use VcVarsAll/VsDevCmd

Long story shorts, those “helpers” provided by Visual Studio to setup your development environment are horribly slow, and seems to get slower on every new version. So, if you’re like me and don’t like to wait 2 to 10 seconds every single time you need a working environment in which you can call cl, then the following might interest you.

Those scripts are “helpers” which will ultimately invoke the one which actually setups the environment so that you can invoke the compiler from the command line, but the thing is they run a lot of other scripts which have nothing to do which this (if you’re curious, see the vsdevcmd\ext folder’s content, everything in it is executed…)

So to “speed things up” (still slow for something that just sets a few environment variables…) you can directly invoke the extension that only sets the Visual C(++) compiler in that folder (vcvars.bat) And since it’s a pain in the ass because it’s not meant to be called as a standalone, here is the minimum amount of boilerplate that I had to write to be able to call it:

@echo off

:: Configure for x64
set VSCMD_ARG_HOST_ARCH=x64
set VSCMD_ARG_TGT_ARCH=x64
set VSCMD_ARG_APP_PLAT=Desktop

:: The version of visual studio you're using (needed for the following scripts to work)
:: Fun fact: if you remove the trailing slash, things stop working. Nice, right?
set VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio\2022\Community\
:: If you're using the Visual Studio Build Tools instead of the IDE:
:: set VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\

:: Setup the MSVC compiler and the Windows SDK
call "%VSINSTALLDIR%\Common7\Tools\vsdevcmd\ext\vcvars.bat"
call "%VSINSTALLDIR%\Common7\Tools\vsdevcmd\core\winsdk.bat"

:: The previous scripts in the past would correctly set the INCLUDE env var. But at some point, they
:: stopped doing so (somewhere between 17.2 and 17.3...) That env var is now set by the main script
:: which the whole point of this is to avoid calling. So here we are... until it breaks again...
if not defined INCLUDE set INCLUDE=%__VSCMD_VCVARS_INCLUDE%%__VSCMD_WINSDK_INCLUDE%%__VSCMD_NETFX_INCLUDE%%INCLUDE%

On my machine, calling this takes approximately 150ms, while calling the usual VsDevCmd.bat takes more than 2 seconds…

One thought on “Don’t use VcVarsAll/VsDevCmd

  1. Thank you so much for taking the time to figure this out and post it! :)
    I must no longer sacrifice hours of my life each year to vcvars.bat 🙅‍♂️

Leave a Reply

Your email address will not be published. Required fields are marked *