【问题标题】:Having trouble compiling in VS Code terminal, which is Windows Powershell在 VS Code 终端(即 Windows Powershell)中编译时遇到问题
【发布时间】:2019-10-22 14:32:59
【问题描述】:

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

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。任何帮助将不胜感激。

【问题讨论】:

  • 当我将“&&”更改为“;”时我不再收到错误,但我确实得到了与我预期不同的结果,这是我需要更改 json 文件的东西吗?

标签: c++ powershell visual-studio-code g++


【解决方案1】:

当您运行程序时,您只需查看正在运行的终端(如果 Windows Powershell 正在运行),那么您应该从终端窗口中“+”按钮旁边的下拉菜单中选择它作为默认终端,并且相同的过程其他终端。

【讨论】:

    【解决方案2】:

    你可以直接在VS代码中下载这个名为Terminal的扩展来解决这个问题

    【讨论】:

      【解决方案3】:

      截至 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。

      【讨论】:

      • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
      • 请尽量使您的帖子可读。这次我为你修好了,但下次请参考 Stack Overflow 的 stackoverflow.com/help/formatting 指南寻求帮助。
      【解决方案4】:

      而不是 && 使用 |它适用于任何语言

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题

        PS:

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

        在 line:1 char:18 tsc testclass.ts && 节点 testclass.js ~~ 标记“&&”在此版本中不是有效的语句分隔符。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine

        PS C:\typescript_practice> tsc testclass.ts |节点 testclass.js 桑加梅什·瓦斯特拉德

        在下面工作

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

        【讨论】:

        • 使用管道符号
        • |&&; 之间的行为不同。我认为分号 ; 按顺序执行命令,相当于 &&
        【解决方案6】:

        在最新更新的 VScode 终端上也遇到过同样的问题, 用 CMD 试过同样的方法,效果很好。 因此,请尝试在其他 shell 或 cmd 而不是 VSCode 终端中运行该命令。

        【讨论】:

        • 在不诊断为什么不工作的情况下随意改变环境不是解决方案。这是一种解决方法。 OP 正在寻找解决他的问题的方法。
        【解决方案7】:

        好的,问题是VS Code默认终端是Windows Powershell 因此,您必须将默认终端更改为 Command Prompt

        1. 点击下拉菜单,在 + 符号旁边显示 Powershell 以表示新终端
        2. 点击选择默认终端
        3. 您将看到可供选择的选项,选择 命令提示符

        然后这些命令将是可执行的

        【讨论】:

          【解决方案8】:

          按照this article 的步骤,我在尝试在 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 上此错误的最高帖子,并且将来可能会对某人有所帮助

          【讨论】:

            【解决方案9】:

            我也遇到了同样的问题,相信是新的 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++ 练习 36.cpp -o 练习 36 } ; if ($?) { .\Exercise36}

            对我来说,使用此命令编译和运行也适用于新的 VS 版本。

            【讨论】:

              【解决方案10】:

              只是猜测您可能需要什么:

              # 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"
              

              如果要检查命令的结果,可以使用$? variable

              【讨论】:

              • 谢谢,我会试一试的。
              猜你喜欢
              • 2011-04-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多