How Can I Access cl.exe in a Script Without Using Developer PowerShell?

0
1
Asked By CleverPineapple42 On

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

Answered By CuriousCabbage75 On

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`.

EnthusiasticEngineer88 -

How do you check the registry for the Visual Studio install location?

Answered By CodeCrafter99 On

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!

CodingConnoisseur31 -

Just to clarify, is this for the MSVC compiler?

Answered By SkepticalScripter12 On

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.

Answered By NewbieNerd On

What exactly is ‘Developer PowerShell’? 🤔

HelpfulHacker11 -

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.

Answered By ResourcefulRambling On

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.

CriticalThinker44 -

True, but if users don't know their installed version, it complicates things.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.