簡體   English   中英

如何在 PowerShell 上使用 cl 命令?

[英]How can I use the cl command on PowerShell?

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

我嘗試將以下命令添加到我的 PowerShell 配置文件以 exec vcbuildtools.bat ,但 PowerShell 無法識別 PowerShell 上的cl命令?

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

操作系統:Windows 10

為了清楚起見,我正在解決提問者的問題,即即使在 PowerShell 中運行它之后, cl也不在 PATH 中

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

我認為這歸結為批處理文件無法將變量導出到 PowerShell (也相關: this question )的問題,正如您在vcbuildtools.bat發現的那樣。 我認為這是因為 PowerShell 調用 cmd.exe 子外殼來執行批處理文件,該文件會更改子外殼中的環境,但更改不會傳播到父外殼,即 PowerShell。


解決方案1

一種方法是利用子shell 從父shell 繼承環境這一事實。 因此,如果您在 PowerShell 中運行它,則批處理文件設置的環境將被保留

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

注意`& 該字符必須進行轉義,因為它在 PowerShell 中具有特殊含義。


解決方案2

Pscx 模塊有一個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 庫下載最新版本的 Pscx,您需要PowerShellGet ,它隨 PowerShell 5 一起提供,可作為 PowerShell 3 和 4 的可下載安裝程序使用。
  • 對於使用 PowerShell 1 和 2 的用戶,可以在Codeplex上使用舊版本的 Pscx。

您可以使用以下函數調用 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"

幸運的是,VS 2019 社區現在有一個用於 VS 2019命令的開發人員 PowerShell

在此處輸入圖片說明

如果您想查看快捷方式的屬性,則實際命令相當冗長。

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 庫中的另一個選項: posh-vs

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

我也遇到了同樣的問題,輸入cmd.exe ,你將控制權更改為命令行。

PowerShell 示例

如果你想回到 PowerShell,沒問題。 只需寫exit 聽起來很簡單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM