Search code examples
c++visual-studio-codevscode-code-runner

C++ not running in vs code


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;
}

Solution

  • 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