在 VS Code 终端(Windows Powershell)中编译时遇到问题

XPM*_*osh 7 c++ powershell g++ visual-studio-code

当我右键单击“运行代码”并在我的用户设置中在终端中运行时,我收到这些错误。

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
Run Code Online (Sandbox Code Playgroud)

直到今天,我一直在编译,我做了一些谷歌搜索并开始在终端中输入我自己的命令,而不仅仅是运行代码。

我开始替换&&with-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
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误。

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
Run Code Online (Sandbox Code Playgroud)

我正在使用 GNU 开源编译器,这是我在编译时遇到的第一个问题,因为我意识到必须在编译之前进行保存。我正在运行一个简单的字符串,它将从 C++ Primer 程序的终端读取的字符串中的所有字符更改为 X。任何帮助,将不胜感激。

小智 10

我有同样的问题

PS:

C:\typescript_practice> tsc testclass.ts && node testclass.js
Run Code Online (Sandbox Code Playgroud)

在 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

与下面一起工作

使用 |而不是&&最新的节点


小智 5

好的,问题是 VS Code 默认终端是Windows Powershell 所以你必须将默认终端更改为命令提示符

  1. 单击下拉菜单,其中除了新终端的+号之外还显示Powershell
  2. 单击选择默认终端
  3. 您将看到可供选择的选项,选择命令提示符

然后这些命令就可以执行了


Osc*_*son 1

我遇到了同样的问题,我相信这是由新的 VS code 版本 1.35 引起的。我尝试降级到 1.34,编译和运行 c++ 再次工作。

旧版本似乎使用不同的命令运行代码:

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

在你的情况下它看起来像:

cd "c:\Users\Josh\Documents\Programming\学习编程\" ; if ($?) { g++Exercise36.cpp -oExercise36 } ; if ($?) { .\Exercise36}

对我来说,使用这个命令编译和运行在新的 VS 版本中也可以工作。