0

I am trying to run my C++ code in vs code. I have installed global extension for C/C++ by Microsoft and also code runner extension. When I run my code it shows this in the terminal.

user@LAPTOP-7LH95TTK MINGW64 ~/Desktop
$ cd "c:\Users\user\Desktop\" && g++ demo.cpp -o demo && "c:\Users\user\Desktop\"demo
bash: cd: c:\Users\user\Desktop" && g++ demo.cpp -o demo && c:UsersuserDesktop"demo: No such file or directory

What should I do ? I think the default command that shows up in the terminal when I run my code is incorrect and I don't know how to change it.

Below is the code from demo.cpp file

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello everyone"<<endl;
    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Rohit Bhati
  • 11
  • 2
  • 4
  • c:UsersuserDesktop"demo: No such file or directory – Serve Laurijssen Jun 16 '21 at 13:41
  • @drescherjm • by "forward slashes" do you mean back slashes? – Eljay Jun 16 '21 at 13:42
  • 1
    I see the problem is VSCode is using back slashes but you are using bash as the shell instead of powershell or cmd.exe in windows. Back slashes in bash mean quote the next character so its trying to change directory to `c:UsersuserDesktop\"` instead of `c:\Users\user\Desktop` – drescherjm Jun 16 '21 at 13:46
  • 1
    The error is likely the backslashes used in tasks.json file like drescherim suggested, you should read https://code.visualstudio.com/docs/cpp/config-mingw, it guides well about the use. – AdityaG15 Jun 16 '21 at 13:51
  • 2
    [why should I not include `bits/std++.h`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – dandan78 Jun 16 '21 at 18:03
  • the backslashes are just from the fact that it's a windows platform and the Code Runner doesn't try to do anything smart with respect to shell quoting behaviour when running in the terminal. rather unfortunate. – starball Apr 05 '23 at 05:16

2 Answers2

1

You can read through https://code.visualstudio.com/docs/cpp/config-mingw (if using MinGw).

If you have MSVC (MS C++ Compiler) installed, instead of 'g++' command you would be using the 'cl' command to compile (the guide for that is at https://code.visualstudio.com/docs/cpp/config-msvc).

Just a brief of the article:

You should have a tasks.json file inside .vscode.

It is the file that is used for instructions to build your code

And, there can be a launch.json also, that is used by VS Code for instructions to debug it. (Not Required)

What i generally do, is click this "Create a launch.json" in the Debug tab, which will ask you to chose the toolchain (compiler,...), and create tasks.json, and launch.json for you.

You can then just use "Shift+Ctrl+B" (default), to just build the code. The debugging option is just a plus, you will require sooner or later :D

Create a launch.json file

AdityaG15
  • 290
  • 3
  • 12
  • I am a little conflicted by this answer. It will fix the problem but it did not mention or explain what was going on with the path passed to `cd` or offer a fix for that. This could have been fixed in code runner. With that said I did not downvote this because it does fix the problem but forces you to abandon code runner. – drescherjm Jun 17 '21 at 12:53
  • @drescherjm yes I guess I forgot to mention it here. I originally commented the "reason" (considering he was using the tasks.json, though as now you tell it seems it maybe related to coderunner, which I haven't used, since it mentioned both I 'assumed' it was the C++ extension itself). If you like to edit to add, I have no problem, but i can't as i may still be missing on that code runner part. For the downvoting I don't have any problems, when it has better answers it is better actually they will get up (•‿•) – AdityaG15 Jun 18 '21 at 10:14
0

I think you should download Mingw gcc compiler and install it in your system properly step by step.

  1. click on Advanced setting by right click on My Pc
  2. click on Environment Varaiable
  3. move to System Variable there
  4. take cursor on PATH click
  5. click on Edit
  6. click New
  7. copy the C:\Mingw\bin or where you have installed
  8. Move the new Path to Top
  9. Click ok

Your problem Solved

4b0
  • 21,981
  • 30
  • 95
  • 142
pushplata
  • 1
  • 1