windows - 为什么 powershell 说 cl.exe 不被识别?

我安装了 visual studio,在 cmd 中运行 vcvarsall.bat 后 cl.exe 运行良好,但在 powershell 中它说无法识别。 命令.exe

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

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

最佳答案

注意:

  • 下面假设没有 vcvarsall.ps1 PowerShell 脚本文件,即 PowerShell 等同于 vcvarsall.bat 批处理 文件。

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

    • 如果它不存在,请考虑将以下解决方案之一包装在您可以放置​​在 $PROFILE file 中的 PowerShell 函数 中;例如:

      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 session 。

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


或者,如果您希望在新窗口开始新的 PowerShell session ,请通过 cmd.exestart 命令调用:

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

至于你尝试了什么:

要使批处理文件 vcvarsall.bat 生效,它必须为 当前进程 定义环境变量。

这适用于 cmd.exe,因为批处理文件在那里进程中执行。

相比之下,PowerShell 必须在 cmd.exe child 进程 中运行批处理文件,因为它无法解释批处理文件本身。
但是,调用进程看不到进程中定义的环境变量。

因此,上述解决方案启动了一个辅助。 cmd.exe 首先处理,它为自己定义环境变量,然后启动一个新的 PowerShell session ,继承这些环境变量。

https://stackoverflow.com/questions/72922846/

相关文章:

scrapy - 如何在 scrapy Shell 中使用 scrapy 中间件?

c++ - 参数的值类别在重载决策(或更一般的函数调用处理)的哪一部分中发挥作用?

algorithm - 计算具有整数边和给定斜边的直角三角形

reactjs - 在进行客户端查询时,我应该如何为 Github graphql API 提供身份

c++ - 为什么 std::shared_ptr 控制 block 需要持有指向具有正确类型的托管

Python 修补类 - 方法 return_value 返回 MagicMock

python - 名称错误 : name 'scipy' is not defined when t

github-pages - 自定义 GitHub 页面部署说明

reactjs - eslint 无法加载配置 "react-app"以从中扩展

java - 什么被认为是 Java 核心模块?