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

Viewed 13474

When I right click Run code, and have run in terminal on in my user settings I get these errors.

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

I have been compiling fine up until today, and I did some googling and started typing my own commands into the terminal instead of just running code.

I started replacing the && with -and and get a different problem. This is what the command looks like now.

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

This is the error I get.

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

I am using the GNU open source compiler, this is the first issue I have had with compiling since I realized you have to save before you compile. I am running a simple string that changes all the characters to X from a string that is read from terminal from C++ Primer program. Any help would be appreciated.

10 Answers

I had the same problem

PS:

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

At line:1 char:18 tsc testclass.ts && node testclass.js ~~ The token '&&' is not a valid statement separator in this version. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine

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

worked with below

use | instead of && in latest for node

Okay, the problem is that VS Code default terminal is Windows Powershell So you have to change your default terminal to Command Prompt

  1. Click on the Dropdown which shows Powershell besides the + sign for new terminal
  2. Click on Select Default Terminal
  3. You will see options to select from, select Command Prompt

Then those commands will be executable

Only a guess what you might need:

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

You can execute these three commands if you divide them with a ;:

   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"

If you want to check the outcome of a command you can use the $? variable.

I had the same problem, I believe it is caused by the new VS code version 1.35. I tried downgrading to 1.34 and compiling and running c++ worked again.

The old version seems to run the code with a different command:

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

in your case it would look like:

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

For me using this comand compiling and running works in the new VS version aswell.

Following the steps from this article, I came across this issue when tried to run the following command inside the terminal on VS code version 1.39.2

npm run build && npm run start-sw

Error:

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

resolution was to run the same command in the Node JS command window;

Adding this here since it was the top post for this error on stackOverflow and may help someone in the future

instead of && use | it works for any language

Had faced same issue with VScode terminal with latest updates, Tried the same with CMD, it worked well. Hence, give a try running the command in other shell, or cmd instead of VSCode terminal.

As of 9/2021, for anyone getting the "&&" error, the previous way did not work for me.

  1. Go to "View" -> select "Command Palette"
  2. Enter "terminal:Select Default Profile"
  3. Set to command prompt

Try this solution instead:

  1. Go to Settings
  2. In the search settings box, type "@feature:terminal"
  3. Find "Terminal > Integrated > Automation Shell:Windows
  4. Click "Edit in settings.json"
  5. Set the
    "terminal.integrated.automationShell.windows"
    
    to
    "C:\\windows\\System32\\cmd.exe"
    

In this way, the default terminal to run "Run C++ Program:g++.exe" becomes cmd.

You can directly download this extension named Terminal in VS code to solve this problem

When you run your program you just have to see what terminal is running if windows powershell is running then you should select it as the default terminal from the dropdown menu next to the '+' button in the terminal window and same process for other terminals.

Related