【问题标题】:How can I use the cl command on PowerShell?如何在 PowerShell 上使用 cl 命令?
【发布时间】:2017-06-15 09:12:09
【问题描述】:

我无法在 PowerShell 中使用 cl 命令。

我尝试将以下命令添加到我的 PowerShell 配置文件以执行 vcbuildtools.bat,但 PowerShell 无法识别 PowerShell 上的 cl 命令?

&"C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"

操作系统:Windows 10

【问题讨论】:

  • 您遇到错误了吗?它说什么(确切地说)?您的PATH 环境变量中是否包含cl.exe 的目录?
  • 如果遇到错误,请发布错误。
  • 启动power shell时没有错误,但无法识别cl命令
  • 如果没有错误,你怎么知道脚本不能识别cl
  • 尽管我在 powershell 上输入了cl 命令,但什么也没发生。

标签: windows powershell windows-10


【解决方案1】:

为了清楚起见,我正在解决提问者的问题,即cl 不在 PATH 中,即使在 PowerShell 中运行它之后也是如此

&"C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"

我认为这归结为batch file can't export variables to PowerShell(也相关:this question)的问题,正如您在vcbuildtools.bat 中发现的那样。我认为这是因为 PowerShell 调用 cmd.exe 子shell 来执行批处理文件,该批处理文件更改了子shell 中的环境,但更改不会传播到父shell,即PowerShell。


解决方案 1

一种方法是使用 subshel​​l 从父 shell 继承环境这一事实。所以如果你在 PowerShell 中运行它,批处理文件设置的环境会被保留

cmd.exe /k "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" `& powershell

注意`&。该字符必须被转义,因为它在 PowerShell 中具有特殊含义。


解决方案 2

Pscx module 有一个 Import-VisualStudioVars 函数,用于导入 Visual Studio 的环境变量。一个示例用法是

Import-VisualStudioVars 2015 amd64

如果您使用 VS/BuildTools 2015 并编译 64 位程序。您可以使用Pop-EnvironmentBlock 还原更改。请参阅man Import-VisualStudioVars -full 了解更多信息。

另外,Pscx 还有一个Invoke-BatchFile 函数,它通过批处理文件保留环境更改。使用示例

Invoke-BatchFile "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"

更多信息请参见man Invoke-Batchfile -full

注意事项

  • 要从 PowerShell gallery 下载最新版本的 Pscx,您需要 PowerShellGet,它随 PowerShell 5 一起提供,可作为 PowerShell 3 和 4 的可下载安装程序提供。
  • 对于那些使用 PowerShell 1 和 2 的用户,可以在 Codeplex 上获得旧版本的 Pscx。

【讨论】:

  • 谢谢:这解决了我从 powershell 编译 C 的问题,我能够运行: cmd.exe /k "C:\Program Files (x86)\Microsoft Visual St udio 14.0\Common7\Tools \vsvars32.bat" `& powershell 然后用头文件编译我的c代码
【解决方案2】:

您可以使用以下函数调用 cmd.exe shell 脚本(批处理文件)并持久化其环境变量:

function Invoke-CmdScript {
  param(
    [String] $scriptName
  )
  $cmdLine = """$scriptName"" $args & set"
  & $env:SystemRoot\system32\cmd.exe /c $cmdLine |
  Select-String '^([^=]*)=(.*)$' | ForEach-Object {
    $varName = $_.Matches[0].Groups[1].Value
    $varValue = $_.Matches[0].Groups[2].Value
    Set-Item Env:$varName $varValue
  }
}

将此函数添加到您的 PowerShell 配置文件,并使用该函数运行批处理文件:

Invoke-CmdScript "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\vsvars32.bat"

【讨论】:

  • 很好的答案,谢谢。这对我有用,而 IT-Rafa 的“解决方案 1”没有,这将在调用 BAT 后停止执行我的 powershell 脚本而没有错误。
【解决方案3】:

幸运的是,VS 2019 社区现在有一个 Developer PowerShell for VS 2019 命令。

如果您想查看快捷方式的属性,实际的命令相当冗长。

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 14bbfab9}"

无论如何,我正在使用它,它会将正确的 cl.exe 添加到我的路径中,但运行它后有一条奇怪的消息:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\ostream(750): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
.\hey.cpp(4): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)' being compiled

【讨论】:

  • 非常感谢!这是此页面中唯一对我有用的东西。我没有管理员。唯一需要考虑的问题是,如果您想在 PowerShell 中运行此命令,您需要将其重写为:&amp;"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -noe -c "&amp;{Import-Module 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell 14bbfab9}" 之后我没有错误....
【解决方案4】:

PowerShell 库中的另一个选项: posh-vs

使 Visual Studio 命令行工具在 PowerShell 中可用。支持 Visual Studio 2017 和 2015。

【讨论】:

    【解决方案5】:

    我也遇到了同样的问题,输入cmd.exe,你就会把控制切换到命令行。

    PowerShell example

    如果您想返回 PowerShell,没问题。只需写exit。听起来很简单

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 2017-02-27
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      相关资源
      最近更新 更多