簡體   English   中英

在 VS Code 終端(Windows Powershell)中編譯時遇到問題

[英]Having trouble compiling in VS Code terminal, which is Windows Powershell

當我右鍵單擊“運行代碼”並在我的用戶設置中的終端中運行時,我收到這些錯誤。

 At line:1 char:63 + ... "c:\\Users\\Josh\\Documents\\Programming\\Learning to Program\\" && g++ Exe ... + ~~ The token '&&' is not a valid statement separator in this version. At line:1 char:99 + ... \\Learning to Program\\" && g++ Exercise36.cpp -o Exercise36 && "c:\\Use ... + ~~ The token '&&' is not a valid statement separator in this version. At line:1 char:102 + ... ercise36 && "c:\\Users\\Josh\\Documents\\Programming\\Learning to Program\\ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Expressions are only allowed as the first element of a pipeline. At line:1 char:160 + ... "c:\\Users\\Josh\\Documents\\Programming\\Learning to Program\\"Exercise36 + ~~~~~~~~~~ Unexpected token 'Exercise36' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine

直到今天,我一直在編譯,我做了一些谷歌搜索並開始在終端中輸入我自己的命令,而不僅僅是運行代碼。

我開始用-and替換&&並遇到不同的問題。 這就是命令現在的樣子。

"c:\Users\Josh\Documents\Programming\Learning to Program\" -and g++ Exercise36.cpp -o Exercise36 -and "c:\Users\Josh\Documents\Programming\Learning to Program\"Exercise36

這是我得到的錯誤。

 Set-Location : Parameter cannot be processed because the parameter name 'o' is ambiguous. Possible matches include: -OutVariable -OutBuffer. At line:1 char:87 + ... \\Programming\\Learning to Program\\" -and g++ Exercise36.cpp -o Exercis ... + ~~ + CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.SetLocationCommand

我正在使用 GNU 開源編譯器,這是我在編譯時遇到的第一個問題,因為我意識到必須在編譯之前進行保存。 我正在運行一個簡單的字符串,它將從 C++ Primer 程序的終端讀取的字符串中的所有字符更改為 X。 任何幫助,將不勝感激。

我有同樣的問題

PS:

C:\typescript_practice> tsc testclass.ts && node testclass.js

在 line:1 char:18 tsc testclass.ts && node testclass.js ~~ 標記 '&&' 在此版本中不是有效的語句分隔符。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException +fullyQualifiedErrorId : InvalidEndOfLine

PS C:\\typescript_practice> tsc testclass.ts | 節點 testclass.js Sangamesh Vastrad

與下面一起工作

使用| 而不是&&在最新的節點

好的,問題是 VS Code 默認終端是Windows Powershell所以你必須把你的默認終端改成Command Prompt

  1. 單擊顯示Powershell下拉菜單,除了新終端的+
  2. 點擊選擇默認終端
  3. 您將看到可供選擇的選項,選擇命令提示符

然后這些命令將是可執行的

僅猜測您可能需要什么:

# change to the directory, where your source code resides
Set-Location "c:\Users\Josh\Documents\Programming\Learning to Program\" 

# Invoke compiler via the call operator = &
& g++ Exercise36.cpp -o Exercise36 

# change to the directory, where your binary resides
Set-Location "c:\Users\Josh\Documents\Programming\Learning to Program\Exercise36"

如果用;分隔它們,則可以執行這三個命令;

   Set-Location "c:\Users\Josh\Documents\Programming\Learning to Program\"; & g++ Exercise36.cpp -o Exercise36; Set-Location "c:\Users\Josh\Documents\Programming\Learning to Program\Exercise36"

如果要檢查命令的結果,可以使用$? 變量

我有同樣的問題,我相信它是由新的 VS 代碼版本 1.35 引起的。 我嘗試降級到 1.34 並再次編譯和運行 c++。

舊版本似乎使用不同的命令運行代碼:

"cd $dir ; if ($?) { g++ $fileName -o $fileNameWithoutExt } ; if ($?) { .\\\\$fileNameWithoutExt}"

在你的情況下,它看起來像:

cd "c:\\Users\\Josh\\Documents\\Programming\\Learning to Program\\" ; if ($?) { g++ Exercise36.cpp -o Exercise36 } ; 如果 ($?) { .\\Exercise36}

對我來說,在新的 VS 版本中使用這個命令編譯和運行也是有效的。

按照本文中的步驟,當我嘗試在 VS 代碼版本 1.39.2 的終端內運行以下命令時遇到了這個問題

npm run build && npm run start-sw

錯誤

 At line:1 char:15 + npm run build && npm run start-sw + ~~ The token '&&' is not a valid statement separator in this version. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine

解決方案是在 Node JS 命令窗口中運行相同的命令;

在此處添加此內容,因為它是 stackOverflow 上此錯誤的最高帖子,將來可能會對某人有所幫助

而不是 && 使用 | 它適用於任何語言

使用最新更新的 VScode 終端遇到了同樣的問題,在 CMD 上嘗試過同樣的問題,效果很好。 因此,嘗試在其他 shell 或 cmd 而不是 VSCode 終端中運行該命令。

截至 2021 年 9 月,對於任何收到“&&”錯誤的人,以前的方法對我不起作用。

  1. 轉到“查看”-> 選擇“命令面板”
  2. 輸入“終端:選擇默認配置文件”
  3. 設置為命令提示符

試試這個解決方案:

  1. 前往設置
  2. 在搜索設置框中,輸入“@feature:terminal”
  3. 找到“終端>集成>自動化外殼:Windows
  4. 單擊“在 settings.json 中編輯”
  5. 設置
    "terminal.integrated.automationShell.windows"
    "C:\\\\windows\\\\System32\\\\cmd.exe"

這樣,運行“Run C++ Program:g++.exe”的默認終端就變成了cmd。

可以直接在VS代碼中下載這個名為Terminal的擴展來解決這個問題

暫無
暫無

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

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