I'm working on a script that needs to call `cl.exe`, but I've run into an issue. The script can only use `cl.exe` when run through Developer PowerShell, as it's not in the system path otherwise. I'd like to avoid asking users to run the script in Developer PowerShell. Is there a way to make the script access `cl.exe` directly?
5 Answers
You could find the exact location of `cl.exe` yourself and run it by specifying the full path. A solid approach would be checking the Windows registry for the installed location of Visual Studio and doing a file search. But if you want a quicker solution, you can just assume it’s under `Program Files`. Here’s a quick PowerShell snippet you can use: `$FilePath = Get-ChildItem 'C:Program Files', 'C:Program Files (x86)' -Recurse -Filter cl.exe | Select-Object -First 1 -ExpandProperty FullName`. Then you can simply execute it with `& $FilePath`.
Are you referring to the C/C++ compiler that isn’t part of .NET? You might want to check out the batch file that sets up the development environment. Look at what environment variables it sets, as you may need to replicate that in your script. I’m skeptical that just having the path to `cl.exe` will be enough; we need that batch file setup too!
Just to clarify, is this for the MSVC compiler?
You should definitely look at the batch file that runs the Visual Studio command prompt and make a copy of it for your purposes. There are likely other environment variables you’ll need to set for it to work properly.
What exactly is ‘Developer PowerShell’? 🤔
It's a command prompt specifically for Visual Studio tools – here’s more info: https://learn.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell.
Another option is to have users add `cl.exe` to their PATH. You could also run the command with the full path specified instead of just `cl.exe`. This is a pretty common problem; here are a couple of StackOverflow threads that discuss it: one about registering an executable to run from any command line and another on retrieving the path to `cl.exe`. Just a heads-up, the downside of modifying the PATH is that the exact Visual Studio version might not be known at runtime.
True, but if users don't know their installed version, it complicates things.
How do you check the registry for the Visual Studio install location?