【问题标题】:Why does powershell say cl.exe is not recognized?为什么 powershell 说 cl.exe 不被识别?
【发布时间】:2022-11-26 09:07:31
【问题描述】:

我安装了 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

以上发生在权力。有谁知道为什么会发生这种情况或如何解决?

【问题讨论】:

    标签: windows visual-studio powershell cl


    【解决方案1】:

    笔记:

    • 以下假设也没有 vcvarsall.ps1电源外壳脚本文件,即 PowerShell 等同于 vcvarsall.bat文件。

    • 如果 vcvarsall.ps1 存在,只需调用它即可(.cvarsall.ps1 如果位于当前目录中,如果位于其中一个目录中则仅调用 vcvarsall.bat。在 $env:PATH 中)

      • 如果不存在,请考虑将以下解决方案之一包装在 PowerShell function 中,您可以将其放入 $PROFILE file;例如。:

        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 会话,它继承那些环境变量。

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 2021-03-05
      • 1970-01-01
      相关资源
      最近更新 更多