Skip to content

Instantly share code, notes, and snippets.

@nickav
Created November 28, 2024 16:00
Show Gist options
  • Save nickav/063b0ef213aea3896a0cd7da5f563464 to your computer and use it in GitHub Desktop.
Save nickav/063b0ef213aea3896a0cd7da5f563464 to your computer and use it in GitHub Desktop.

Instant MSVC Command Prompt Sessions

If you do C programming on Windows you need cl.exe in your PATH.

Unfortunately, running the batch file to locate the cl.exe compiler (vcvarsall.bat) takes about ~5 seconds!

That's only a problem on new Command Prompt sessions, but if you use a program that kicks off a build step it's really annoying to have to run this before any of your build commands. But, thankfully, there's a way around this. We can create a script to inflate the same environment variables that vcvarsall.bat ultimately sets up.

As a side-effect, this is also how to have a single file to manage all of your Command Prompt environment variables in Windows.


0. Install MSVC

To install cl.exe you need to download and install Microsoft Visual Studio Community Edition.

Go through the install flow and check C++ compiler tools.

If you want, you can also install the clang compiler.

1. Create the environment variables file

Locate the vcvarsall.bat script. You can find this script in a path that looks like this (note that your version number may be different):

C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat

Now, navigate to a folder where you will store the script (for example, C:\dev\dotfiles\windows).

cd C:\dev\dotfiles\windows

Load the required environment variables by invoking the script:

"C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64

Generate the post.env file:

set > post.env

Create a new batch file with these contents:

:: File: env.bat

@echo off

set dotfiles_path=%~dp0%

:: generate with: set > post.env
FOR /F "tokens=*" %%i in (%dotfiles_path%\post.env) do set %%i

To verify this was set up correctly, you should be able to open a new Command Prompt and run the env.bat:

C:\dev\dotfiles\windows\env.bat

Then run:

where cl.exe

That should return a path to the cl.exe compiler.

2. Auto run this file on every Command Prompt session

To get the env.bat file to run on every new Command Prompt session, you'll have to edit the registry.

Run (Win+R) regedit and navigate to the HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor folder.

Right-click and add a String Value entry with the name AutoRun and set the value to the full path of your .bat/.cmd file (e.g. C:\dev\dotfiles\windows\env.bat).

3. Done!

If this was all done properly, you should be able to start a new Command Prompt and just type:

cl.exe /help

NOTE: you won't be able to edit your PATH through the normal windows UI anymore, instead you can just edit the post.env file directly.

Personally, I think it's much nicer to be able to edit one file with all my environment variables anyway, so this is a win-win!

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