为什么PowerShell会说无法识别cl.exe?

3

我已经安装了Visual Studio,当在cmd中运行vcvarsall.bat后,cl.exe可以完美运行,但在PowerShell中提示无法识别。

C:\Users\Ethos>vcvarsall.bat x64
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.2.5
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Users\Ethos>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.32.31332 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

以上在cmd中表现良好。

PS C:\Users\Ethos> vcvarsall.bat x64
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.2.5
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
PS C:\Users\Ethos> cl
cl : The term 'cl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ cl
+ ~~
    + CategoryInfo          : ObjectNotFound: (cl:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

上述情况发生在电源方面。有人知道为什么会发生这种情况或如何解决吗?

2个回答

2
注意:
  • The following assumes that there isn't also a vcvarsall.ps1 PowerShell script file, i.e. the PowerShell equivalent to the vcvarsall.bat batch file.

  • If vcvarsall.ps1 exists, simply call it instead (.\vcvarsall.ps1 if located in the current dir, just vcvarsall.bat if located in one of the dirs. in $env:PATH)

    • If it doesn't exist, consider wrapping one of the solutions below in a PowerShell function that you can place in your $PROFILE file; e.g.:

      function vcvarsall {
        cmd /c vcvarsall.bat @args '&&' (Get-Process -Id $PID).Path        
      }
      

从PowerShell中运行以下命令(假设vcvarsall.bat要么在当前目录中,要么在列在$env:PATH中的目录中):

cmd /c vcvarsall.bat x64 '&&' (Get-Process -Id $PID).Path

这会指示cmd.exe定义环境变量,然后启动另一个PowerShell会话,该会话继承了这些环境变量。

请注意,原始的PowerShell会话将继续存在(就像cmd.exe一样,但在嵌套的PowerShell会话结束时自动退出),因此当您exit嵌套的PowerShell会话时,将返回到原始会话。


或者,如果您喜欢在新窗口中启动新的PowerShell会话,请通过cmd.exestart命令调用:

cmd /c "vcvarsall.bat x64 && start `"$([Console]::Title)`" `"$((Get-Process -Id $PID).Path)`""

关于您尝试的内容
对于批处理文件vcvarsall.bat,它必须为当前进程定义环境变量才能生效。
这在cmd.exe中有效,因为批处理文件在那里执行内部进程
相比之下,PowerShell必须在cmd.exe进程中运行批处理文件,因为它本身不能解释批处理文件。
然而,进程中定义的环境变量不会被调用进程看到
因此,上述解决方案首先启动一个辅助的cmd.exe进程,该进程为自身定义环境变量,然后启动一个新的PowerShell会话,该会话继承这些环境变量。

0

这是旧的做法。

请安装并开始使用 Windows Terminal (WT) 和 Powershell Core (pwsh.exe),然后 WT 将自动设置正确的路径到 shell 的 Dev 版本。

如果没有,您可以修改 pwsh 快捷方式为:

"C:\Program Files\PowerShell\7\pwsh.exe" -NoExit -Command "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell f9d021a0 -SkipAutomaticLocation -DevCmdArguments """-arch=x64 -host_arch=x64"""}"

或者手动运行它(或将其添加到您的PowerShell配置文件):

Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"

Enter-VsDevShell f9d021a0 -SkipAutomaticLocation -DevCmdArguments "-arch=x64 -host_arch=x64"

这应该能帮助你完成设置!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接