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.
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.
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\windowsLoad the required environment variables by invoking the script:
"C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64Generate the post.env file:
set > post.envCreate 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 %%iTo 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.batThen run:
where cl.exeThat should return a path to the cl.exe compiler.
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).
If this was all done properly, you should be able to start a new Command Prompt and just type:
cl.exe /helpNOTE: 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!